django 性能优化_优化Django管理员
django 性能優(yōu)化
Managing data from the Django administration interface should be fast and easy, especially when we have a lot of data to manage.
從Django管理界面管理數(shù)據(jù)應(yīng)該快速簡(jiǎn)便,尤其是當(dāng)我們要管理大量數(shù)據(jù)時(shí)。
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.
為了改進(jìn)該過(guò)程并使您更容易理解,我們將在一個(gè)項(xiàng)目中使用的真實(shí)場(chǎng)景在這里工作。
情境 (Scenario)
The scenario is adding products from Django administration for an ecommerce website.
該方案是為電子商務(wù)網(wǎng)站添加來(lái)自Django管理的產(chǎn)品。
We have three tables: Category, Subcategory, and Product.
我們有三個(gè)表:類(lèi)別,子類(lèi)別和產(chǎn)品。
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.
每個(gè)產(chǎn)品都與一個(gè)類(lèi)別和一個(gè)子類(lèi)別相關(guān),后者又與一個(gè)類(lèi)別相關(guān)。 您可以在下面看到所有三個(gè)表的模型。
The product has these fields to fill: Name, Slug, Category, subcategory (which should be related to one category), and Description.
產(chǎn)品具有以下字段要填寫(xiě):名稱(chēng),子類(lèi)別,類(lèi)別,子類(lèi)別(應(yīng)與一個(gè)類(lèi)別相關(guān))和描述。
While inserting a new product, there are two select dropdown fields (Category and Subcategory).
插入新產(chǎn)品時(shí),有兩個(gè)選擇下拉字段(類(lèi)別和子類(lèi)別)。
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.
這里的問(wèn)題是:當(dāng)我填寫(xiě)名稱(chēng)和子詞然后選擇要添加的產(chǎn)品的類(lèi)別時(shí),我會(huì)在子類(lèi)別的下拉列表中獲得所有項(xiàng)目,甚至是不屬于該類(lèi)別的項(xiàng)目已選擇。 因此,我需要在下拉子類(lèi)別中填充與所選類(lèi)別相關(guān)的項(xiàng)目。
結(jié)果預(yù)覽 (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:
現(xiàn)在,我將向您展示為解決該問(wèn)題而找到的正確解決方案。 它是如此容易實(shí)現(xiàn)-只需仔細(xì)遵循這三個(gè)步驟。 但是首先,這是我的設(shè)置:
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.
這是三個(gè)表的模型。
類(lèi)別表模型 (Model for category table)
子類(lèi)別表的模型 (Model for subcategory table)
產(chǎn)品表型號(hào) (Model for product table)
解 (Solution)
1.創(chuàng)建視圖 (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 JSONdata.
要填寫(xiě)子類(lèi)別字段下拉菜單中的項(xiàng)目,您必須創(chuàng)建一個(gè)將以JSON數(shù)據(jù)作為響應(yīng)的視圖。
views.py:
views.py :
2.添加圖案 (2. Add pattern)
At urls.py, you need to add a pattern to reach the view:
在 urls.py ,您需要添加一個(gè)模式以到達(dá)視圖:
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.
現(xiàn)在,您必須重寫(xiě)change_form.html 為您的產(chǎn)品應(yīng)用添加Django管理員的代碼,以添加一些JavaScript代碼來(lái)完成此任務(wù)。
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.htmlhas to be the same as the original HTML file name provided by Django.
該文件的位置并不重要。 您可以將其放入您的應(yīng)用程序中,并且仍然可以使用。 只要它的位置可以被Django發(fā)現(xiàn)。 更重要的是HTML文件的名稱(chēng)change_form.html必須與Django提供的原始HTML文件名相同。
change_form.html:
change_form.html :
結(jié)論 (Conclusion)
And just like that, now the items in the list of items in the dropdown select subcategory are related to the category selected.
就像這樣,現(xiàn)在下拉選擇子類(lèi)別中的項(xiàng)目列表中的項(xiàng)目與所選類(lèi)別相關(guān)。
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.
如果您對(duì)此問(wèn)題有其他解決方案,請(qǐng)?jiān)谙旅骐S意提出,以提高我們與Django框架的技能,尤其是使Django管理界面成為更強(qiáng)大的工具。
翻譯自: https://medium.com/better-programming/optimizing-django-admin-6a1187ddbb09
django 性能優(yōu)化
總結(jié)
以上是生活随笔為你收集整理的django 性能优化_优化Django管理员的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 做梦梦到别人生小孩子什么意思呢
- 下一篇: 数据质量提升_合作提高数据质量