Django - Too slow database record read - python

I have a database with about 1500 records about one class. I'm also using jQuery Datatables to list the records.
views.py:
records = Animal.objects.all()
return render(request, 'animals.html', {'records': records})
template:
{% for record in records%}
<tr>
<td>{{ record.id }}</td>
<td>{{ record.specie }}</td>
<td>{{ record.category}}</td>
<td>{{ record.quantity }}</td>
</tr>
{% endfor %}
But, it is very very slow. Even reducing the number of records to 100 it takes about 20~45 seconds to fully load a page. Doing the same select to find all 1500 records directly on database takes only 1 second.
As I am using jQuery DataTables to paginate and search the values, is there a way to improve this loading table time?
EDIT:
I used the django debug toolbar and I got these results:

Related

passing variable to a table in flask

I am following this video and everything is working perfectly. The output of a SQL query is saved within a variable that I can call on in another .html page.
so my sql query is:
#app.route('/all_data')
def all_data():
customers = db.get_customers()
for customer in customers:
var = customers
return render_template('all_data.html',var=var)
When calling this {{var}} in all_data.html page the output is for a long tuple:
('name','email','comment','gender'),('name','email','comment','gender') etc etc
I am looking to put this {{var}} into a table instead?.. I but am hitting a brick wall in finding out how to do this?
any help would be much appreciated!
Assuming you're using Flask's default template engine (Jinja2), you can simple use a for loop to iterate over the elements of the collection inside the template. The syntax is:
{% for item in items %}
{{ item }}
{% endfor %}
So simply create an HTML table, and add a row for each item in your var variable.
IIRC, Jinja tuples act the same as Python's, so you can probably do something along the lines of:
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Comment</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
{% for current in var %}
<tr>
<td>{{ current[0] }}</td>
<td>{{ current[1] }}</td>
<td>{{ current[2] }}</td>
<td>{{ current[3] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
I haven't checked it (as I don't have a Flask project available right now), but if my memory serves me right, that's how I would do it
Here's a link to an usefull part of Jinja2's documentation
Also, what is this piece of code for ?
for customer in customers:
var = customers
You're iterating over each element of the list, but your never use the customer variable (containing the current element). Instead, you assign the var variable to the customers list multiple times. Assuming customers is a simple Python list, you could remove this loop and simply do return render_template('all_data.html',var=customers)

Using django-auditlog, how can I display the 'actor_id' for a particular model?

I have created a simple Django application to display individual articles. These articles have a number of fields that users can edit. I am using the package 'django-auditlog' to log changes to these article models. So far, I have simply followed the auditlog installation doc to setup model history tracking (as well as enabling the middleware to allow 'actor_id' to be tracked). I have also added the example code that displays the most recent changes on the individual model pages as such:
<!-- History display -->
<div class="table-responsive">
<table id="history" class="table table-striped table-bordered">
<thead>
<tr>
<th>Actor</th>
<th>Field</th>
<th>From</th>
<th>To</th>
</tr>
</thead>
<tbody>
<!-- Human readable - change to '.changes_dict.' for proper logs -->
{% for key, value in article.history.latest.changes_display_dict.items %}
<tr>
<td>{{ article.history.latest.author_id }}</td>
<td>{{ key }}</td>
<td>{{ value.0|default:"None"|striptags|safe }}</td>
<td>{{ value.1|default:"None"|striptags|safe }}</td>
</tr>
{% empty %}
<p>No history for this item has been logged yet.</p>
{% endfor %}
</tbody>
</table>
</div>
As my code may suggest, I am trying to add an additional column to the history table to show who made the changes that are being displayed.
Is there an easy way to do this through auditlog, or will I have to create some kind of sql query to my sqlite auditlog db table to retrieve the 'author_id' field?
Thank you!
I figured out the answer after looking through the models file for Django AuditLog. It is not possible to pull the actor out directly from the history field of the model if you have created the history field using the AuditlogHistoryField() method as described in the django-auditlog tutorial.
Instead, I did the following:
In the views.py file
from auditlog.models import LogEntry
...
dal_log = LogEntry.objects.get_for_object(article)
...
context = {'article': article, 'logs': dal_log}
return render(request, "detail.html", context)
Then in my template, I was able to work with the log entries for the specified object (in my case, these were 'article' models). There may be a cleaner way, but this worked for me.

HTML FLASK using {% for %} in JSON data

I am using google firebase which returns data in JSON, compared to SQLAlchemy which returns it in something else.
In SQLAlchemy, I can fetch a database, pass it as a variable in flask render_template, and do something like this to display all the tables.
{% for Order in query %}
<tr>
<td>{{ Order.uuid }}</td>
<td>{{ Order.costusd }}</td>
<td>{{ Order.costcry }}</td>
<td>{{ Order.orderinfo }}</td>
<td>{{ Order.userpaid }}</td>
</tr>
{% endfor %}
If I attempt this with firebase/JSON data, it will display empty tables (the amount of tables that I have data in firebase) but with no data. I tried to do order['uuid']. etc for all of them, but that does not work either.
So, I am trying to display my database in the same way that SQLAlchemy can do it, but i am using a NOSQL database that is JSON.
This was a duplicate
Working code to translate the NoSQL data into jinja2 readable:
r = json.dumps(json_onject)
loaded_r = json.loads(r)
return render_template("report.html", items=loaded_r)

Django: Accessing list attributes in template

I have a SQL query in a Django view and store the results in a variable. As far I know the result should be stored as a list.
def assignments(request):
cursor = connection.cursor()
cursor.execute("SELECT o.article_id, o.amount, m.id, o.create_date, m.status FROM orders o, producers p, machines ma, matches m WHERE ma.producer_id=1 AND m.machine_id = ma.id AND m.order_id = o.id")
articles = cursor.fetchall()
context = {"article_list": articles}
return render(request, 'assignments.html', context)
Then I want to transfer that data row by row in a table in my template.
{% block body %}
<div class="container">
<table class="table">
<thead>...</thead>
<tbody>
{% for articles in article_list %}
<tr>
<td>{{ articles.article_id }}</td>
<td>{{ articles.amount }}</td>
<td>{{ articles.id}}</td>
<td>{{ articles.create_date }}</td>
<td>{{ articles.status }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
Unfortunately the table is empty and is not showing any results.
The query itself should be fine. I tested the query in my database workbench and it is showing the correct results.
How can I access the data stored in variable articles from my template?
PS: I'm far from being a programmer. So I don't really know any programming concepts/language.
Any help is highly appreciated. Thanks!
You have a list of lists. Each element in articles_list is just a list of values from the database; it does not have any knowledge of attributes like "id" or "amount".
As Arpit says in the comments, you should be using Django models for this rather than raw SQL.

Unicode issues when requesting form

I'm using HTML, Flask and MYSQL to populate a table when the user first loads the page. The table displays all the rows along with a checkbox and when a user presses the submit button, I get the rows that were checked.
Current HTML code
{% for row in data %}
<tr>
<td><input type="checkbox" name="inputSelect" value="{{ row[0], row[3] }}"></td>
<td>{{ row[0] }}</td>
<td>{{ row[1] }}</td>
<td>{{ row[2] }}</td>
<td>{{ row[3] }}</td>
</tr>
{% endfor %}
However, when I try request.form['InputSelect'] on the flask side, it only gives me one result even if I click more than one checkbox.
What's the best way to create the table so that when I click multiple checkboxes, I can see all of them using request.form['InputSelect']
Flask use MultiDict. To get the list of items for a given key, you can use getlist() method.
Try this:
value = request.form.getlist('InputSelect')

Categories

Resources