I have a Django project that interfaces with a PostgreSQL database; one of my tables is about 450 rows long and each row contains about a dozen columns.
我有一个Django项目,它与PostgreSQL数据库接口;我的一个表大约有450行,每一行包含大约12列。
Each column's value must be put into a different formula to produce the final data I want to display in the template layer, but each row will be processed with the same set of calculations. (In other words, I want to perform 12 different calculations 450 times each.)
必须将每个列的值放入不同的公式中,以生成我希望在模板层中显示的最终数据,但是每一行都将使用相同的计算集进行处理。(换句话说,我想做12次不同的计算,每次450次。)
My question is, of the following methods, which is the generally accepted method of doing so, and which is the best in terms of performance:
我的问题是,下列方法中,哪一种是普遍接受的方法,哪一种是最佳的方法:
A) Write each calculation as a model method in models.py, query all objects of interest (again, about 450 of them) in views.py and pass them directly to the template, and then use Django's template language to write things like:
A)将每个计算都写成模型方法。py,在视图中查询所有感兴趣的对象(同样,大约有450个)。py并将它们直接传递给模板,然后使用Django的模板语言编写如下内容:
<table>
{% for item in list %}
<tr>
<td>{{item.name}}</td>
...
</tr>
{% endfor %}
</table>
(where .name
is a model method)
(其中.name是一个模型方法)
B) Peform the query and calculations in the views.py file and pass the results organized into one big dictionary or list or JSON to the template (and then parse it using template tags),
B)将查询和计算放在视图中。py文件并将组织的结果传递到一个大字典或列表或JSON到模板中(然后使用模板标记解析它),
C) Pass all of the objects of interest to the template as in A), but perform calculations on each object's fields using Javascript or jQuery,
C)将所有感兴趣的对象都传递到模板中,但是使用Javascript或jQuery对每个对象的字段执行计算,
D) Combine B) and C) to perform all database queries in views.py and then pass the raw values as a big dictionary/list/JSON object to the template and perform calculations on those values using Javascript/jQuery.
D)合并B)和C)来执行视图中的所有数据库查询。然后将原始值作为大字典/列表/JSON对象传递给模板,并使用Javascript/jQuery对这些值进行计算。
To my mind, method A) seems the least efficient as it requires you to hit the database a ton of times in the template layer. But it makes writing the view and template extremely simple (just requires you to write 12 different model methods). Method B) seems to follow the general tenet of performing as much logic as possible in your view, and simply passing along the final results to be displayed in the template. But it makes the view function long and ugly. Methods C) and D) put most of load on the end user's browser, which seems like it could really take a load off the server, but then obviously won't work if the user has JS turned off.
在我看来,方法A)似乎是效率最低的,因为它要求您在模板层中多次访问数据库。但它使视图和模板的编写极其简单(只需编写12种不同的模型方法)。方法B)似乎遵循在视图中尽可能多地执行逻辑的一般原则,并简单地传递要在模板中显示的最终结果。但它使视图函数变得又长又丑。方法C)和D)将大部分负载放在最终用户的浏览器上,这似乎真的可以减少服务器的负载,但如果用户关闭了JS,显然就不能工作了。
But again, I'd like to know if there's an accepted best practice for this kind of situation, and whether that contradicts the fastest method from a computational standpoint.
但是,我想知道对于这种情况是否有一个公认的最佳实践,以及从计算的角度来看,这是否与最快的方法相矛盾。
And obviously, if I've missed the best method of all, please let me know what it would be.
显然,如果我错过了最好的方法,请告诉我它是什么。
1 个解决方案
#1
1
Methods (C) and (D), though tempting are not the way to go. The client's computer might be slow in which case the page might just freeze their browser while it performs all of these calculations.
方法(C)和(D),虽然诱人,但不是正确的方法。客户机的计算机可能很慢,在这种情况下,页面在执行所有这些计算时可能会冻结浏览器。
(A) seems like the best option, and it shouldn't incur in additional queries. If you pass the objects to the template in a single queryset (Model.objects.all() for example), then Django should perform a single query to fetch all of the objects, then when calling the method in the template no additional query should be performed. If your objects are related to others, and this relation is used in the calculation, make sure to use select_related or prefetch_related because querying a related object on each model call will indeed incur in a single query per object unless you prefetch/select what you're going to use beforehand (https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related).
(A)似乎是最好的选择,不应该在其他查询中出现。如果您将对象传递给一个queryset(例如Model.objects.all())中的模板,那么Django应该执行一个查询来获取所有的对象,那么当调用模板中的方法时,不应该执行任何其他查询。如果您的对象与其他对象相关,并且此关系在计算中使用,请确保使用select_related或prefetch_related,因为在每个模型调用上查询相关对象确实会在每个对象的单个查询中产生,除非您预先获取/选择要使用的对象(https://docs.angoprot.com/en/dev/ref/models/quers/querysets/querts/)。
#1
1
Methods (C) and (D), though tempting are not the way to go. The client's computer might be slow in which case the page might just freeze their browser while it performs all of these calculations.
方法(C)和(D),虽然诱人,但不是正确的方法。客户机的计算机可能很慢,在这种情况下,页面在执行所有这些计算时可能会冻结浏览器。
(A) seems like the best option, and it shouldn't incur in additional queries. If you pass the objects to the template in a single queryset (Model.objects.all() for example), then Django should perform a single query to fetch all of the objects, then when calling the method in the template no additional query should be performed. If your objects are related to others, and this relation is used in the calculation, make sure to use select_related or prefetch_related because querying a related object on each model call will indeed incur in a single query per object unless you prefetch/select what you're going to use beforehand (https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related).
(A)似乎是最好的选择,不应该在其他查询中出现。如果您将对象传递给一个queryset(例如Model.objects.all())中的模板,那么Django应该执行一个查询来获取所有的对象,那么当调用模板中的方法时,不应该执行任何其他查询。如果您的对象与其他对象相关,并且此关系在计算中使用,请确保使用select_related或prefetch_related,因为在每个模型调用上查询相关对象确实会在每个对象的单个查询中产生,除非您预先获取/选择要使用的对象(https://docs.angoprot.com/en/dev/ref/models/quers/querysets/querts/)。