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.pswp.cn/news/388230.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/388230.shtml 英文地址,請注明出處:http://en.pswp.cn/news/388230.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!