django 性能优化
Managing data from the Django administration interface should be fast and easy, especially when we have a lot of data to manage.
从Django管理界面管理数据应该快速简便,尤其是当我们要管理大量数据时。
To improve that process and to make it easier for you to understand, we’re going to work here with a real scenario that I worked through in one of my projects.
为了改进该过程并使您更容易理解,我们将在一个项目中使用的真实场景在这里工作。
情境 (Scenario)
The scenario is adding products from Django administration for an ecommerce website.
该方案是为电子商务网站添加来自Django管理的产品。
We have three tables: Category, Subcategory, and Product.
我们有三个表:类别,子类别和产品。
Each product is related to one category and to one subcategory, which is also related to one category. You can see the models of all three tables below.
每个产品都与一个类别和一个子类别相关,后者又与一个类别相关。 您可以在下面看到所有三个表的模型。
The product has these fields to fill: Name, Slug, Category, subcategory (which should be related to one category), and Description.
产品具有以下字段要填写:名称,子类别,类别,子类别(应与一个类别相关)和描述。
While inserting a new product, there are two select dropdown fields (Category and Subcategory).
插入新产品时,有两个选择下拉字段(类别和子类别)。
The problem here is: When I fill the name and the slug and then select the category of the product that I want to add, I get all the items in the dropdown of the subcategory, even the items that don’t belong to the category selected. So I need to populate the items in the dropdown subcategory which are related to the category selected.
这里的问题是:当我填写名称和子词然后选择要添加的产品的类别时,我会在子类别的下拉列表中获得所有项目,甚至是不属于该类别的项目已选择。 因此,我需要在下拉子类别中填充与所选类别相关的项目。
结果预览 (Preview of the Result)
Now I will show you the right solution that I found to solve that problem. It’s so easy to implement — just follow these three steps carefully. But first, this is my setup:
现在,我将向您展示为解决该问题而找到的正确解决方案。 它是如此容易实现-只需仔细遵循这三个步骤。 但是首先,这是我的设置:
Python →3.7
Python→3.7
Django →2.2.4
Django的→2.2.4
JQuery →3.2.1
jQuery→3.2.1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
And here are the models of the three tables.
这是三个表的模型。
类别表模型 (Model for category table)
子类别表的模型 (Model for subcategory table)
产品表型号 (Model for product table)
解 (Solution)
1.创建视图 (1. Create view)
To fill in the items in the dropdown of the subcategory field, you have to create a view that will respond with JSON
data.
要填写子类别字段下拉菜单中的项目,您必须创建一个将以JSON
数据作为响应的视图。
views.py
:
views.py
:
2.添加图案 (2. Add pattern)
At urls.py
, you need to add a pattern to reach the view:
在 urls.py
,您需要添加一个模式以到达视图:
3.添加JavaScript代码 (3. Add JavaScript code)
Now you have to override change_form.html
of Django admin for your product app to add some JavaScript code to do the magic.
现在,您必须重写change_form.html
为您的产品应用添加Django管理员的代码,以添加一些JavaScript代码来完成此任务。
The location of this file is not important. You can put it inside your app and it will still work. As long as its location can be discovered by Django. What’s more important is the name of the HTML file change_form.html
has to be the same as the original HTML file name provided by Django.
该文件的位置并不重要。 您可以将其放入您的应用程序中,并且仍然可以使用。 只要它的位置可以被Django发现。 更重要的是HTML文件的名称change_form.html
必须与Django提供的原始HTML文件名相同。
change_form.html
:
change_form.html
:
结论 (Conclusion)
And just like that, now the items in the list of items in the dropdown select subcategory are related to the category selected.
就像这样,现在下拉选择子类别中的项目列表中的项目与所选类别相关。
If you have another solution to this problem, feel free to suggest it down below to increase our skills together with the Django framework, especially to make the Django administration interface a more powerful tool to work with.
如果您对此问题有其他解决方案,请在下面随意提出,以提高我们与Django框架的技能,尤其是使Django管理界面成为更强大的工具。
翻译自: https://medium.com/better-programming/optimizing-django-admin-6a1187ddbb09
django 性能优化
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388230.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!