对Django数据库值执行计算:views.py,模板还是Javascript? [英] Performing calculations on Django database values: views.py, template or Javascript?

查看:89
本文介绍了对Django数据库值执行计算:views.py,模板还是Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与PostgreSQL数据库连接的Django项目;我的一张桌子长约450行,每行约有十几列.

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.

必须将每个列的值放入不同的公式中,以生成要显示在模板层中的最终数据,但是每一行都将使用相同的计算集进行处理.(换句话说,我想执行12次不同的计算,每次450次.)

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.)

我的问题是,以下几种方法中,这是公认的方法,并且在性能方面是最好的:

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)在model.py中将每个计算作为模型方法编写,在views.py中查询所有感兴趣的对象(再次,约450个)并将它们直接传递给模板,然后使用Django的模板语言编写像这样的东西:

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:

<table>
{% for item in list %}
    <tr>
         <td>{{item.name}}</td>
         ...
    </tr>
{% endfor %}
</table>

(其中 .name 是模型方法)

B)在views.py文件中执行查询计算,然后将组织成一个大字典或列表或JSON的结果传递给模板(然后使用模板标签进行解析),

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),

C)和A)中一样,将所有感兴趣的对象传递到模板,但是使用Javascript或jQuery对每个对象的字段执行计算,

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,

D)结合B)和C)以在views.py中执行所有数据库查询,然后将 raw 值作为大词典/列表/JSON对象传递给模板,并对这些模板执行计算使用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.

在我看来,方法A)似乎效率最低,因为它要求您在模板层中多次访问数据库.但这使编写视图和模板非常简单(只需要您编写12种不同的模型方法即可).方法B)似乎遵循在您的视图中执行尽可能多的逻辑,并简单地传递最终结果以显示在模板中的一般原则.但这会使视图功能变得冗长而丑陋.方法C)和D)将大部分负载都放在了最终用户的浏览器上,这似乎确实可以减轻服务器的负载,但是如果用户关闭JS,那么显然将不起作用.

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.

但是,再次,我想知道是否存在针对这种情况的最佳实践,从计算的角度来看,这是否与 fastest 方法相矛盾.

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.

推荐答案

方法(C)和(D),虽然诱人并非可行.客户的计算机速度可能很慢,在这种情况下,页面可能会冻结浏览器,同时执行所有这些计算.

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.

(A)似乎是最好的选择,并且不应在其他查询中出现.如果您在单个查询集中(例如Model.objects.all())将对象传递给模板,则Django应该执行单个查询以获取所有对象,然后在模板中调用方法时不应进行其他查询被执行.如果您的对象与其他对象相关,并且在计算中使用了此关系,请确保使用select_related或prefetch_related,因为在每个模型调用上查询相关对象的确会导致对每个对象进行一次查询,除非您预取/选择您要使用的对象重新使用(( https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related ).

(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).

这篇关于对Django数据库值执行计算:views.py,模板还是Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆