django 性能優化_優化Django管理員

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.

產品具有以下字段要填寫:名稱,子類別,類別,子類別(應與一個類別相關)和描述。

Image for post

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)

Django Admin- Best way to populate select options depending on another select

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

要填寫子類別字段下拉菜單中的項目,您必須創建一個將以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.htmlhas 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,一經查實,立即刪除!

相關文章

3D場景中選取場景中的物體。

楊航最近在學Unity3D&#xfeff;&#xfeff;&#xfeff;&#xfeff;在一些經典的游戲中&#xff0c;需要玩家在一個3D場景中選取場景中的物體。例如《仙劍奇俠傳》&#xff0c;選擇要攻擊的敵人時、為我方角色增加血量、為我方角色添加狀態&#xff0c;通常我們使用鼠標來選…

xpath之string(.)方法

from lxml import etreehtml <li class"tag_1">需要的內容1<a>需要的內容2</a></li> selector etree.HTML(html ) contents selector.xpath ( //li[class "tag_1"]) contents1 selector.xpath ( //li[class "tag…

循環語句

循環語句&#xff1a; 當我們要做一些重復的操作時&#xff0c;首先想到的是有沒有一種循環的語句&#xff1f; 答案當然有 Java提供了三種循環&#xff1a; for循環&#xff0c;在Java5中引入了一種主要用于數組的增強型for循環。while循環do……while循環for循環語法1&#x…

canva怎么使用_使用Canva進行數據可視化項目的4個主要好處

canva怎么使用(Notes: All opinions are my own. I am not affiliated with Canva in any way)(注意&#xff1a;所有觀點均為我自己。我與Canva毫無關系) Canva is a very popular design platform that I thought I would never use to create the deliverable for a Data V…

如何利用Shader來渲染游戲中的3D角色

楊航最近在學Unity3D&#xfeff;&#xfeff; 本文主要介紹一下如何利用Shader來渲染游戲中的3D角色&#xff0c;以及如何利用Unity提供的Surface Shader來書寫自定義Shader。 一、從Shader開始 1、通過Assets->Create->Shader來創建一個默認的Shader&#xff0c;并取名…

深入bind

今天來聊聊bind 關于之前的call跟apply 查看此鏈接 我們要明確4點內容 1. bind之后返回一個函數 let obj {name : skr } function fn(){console.log(this) } let bindfn fn.bind(obj) console.log(typeof bindfn) // function 2.bind改變this 并且可以傳參 bind之后的函數仍…

Css單位

尺寸 顏色 轉載于:https://www.cnblogs.com/jsunny/p/9866679.html

ai驅動數據安全治理_JupyterLab中的AI驅動的代碼完成

ai驅動數據安全治理As a data scientist, you almost surely use a form of Jupyter Notebooks. Hopefully, you have moved over to the goodness of JupyterLab with its integrated sidebar, tabs, and more. When it first launched in 2018, JupyterLab was great but fel…

【Android】Retrofit 2.0 的使用

一、概述 Retrofit是Square公司開發的一個類型安全的Java和Android 的REST客戶端庫。來自官網的介紹&#xff1a; A type-safe HTTP client for Android and JavaRest API是一種軟件設計風格&#xff0c;服務器作為資源存放地。客戶端去請求GET,PUT, POST,DELETE資源。并且是無…

一個透明的shader

楊航最近在學Unity3D&#xfeff;&#xfeff;Shader "Custom/xiankuang" { Properties { _LineColor ("Line Color", Color) (1,1,1,1) _GridColor ("Grid Color", Color) (1,1,1,0) _LineWidth ("Line Width", float) 0…

Mysql常用命令(二)

對數據庫的操作 增 create database db1 charset utf8; 查 # 查看當前創建的數據庫 show create database db1; # 查看所有的數據庫 show databases; 改 alter database db1 charset gbk; 刪 drop database db1; 對表的操作 use db1; #切換文件夾select database(); #查看當前所…

python中定義數據結構_Python中的數據結構—簡介

python中定義數據結構You have multiples algorithms, the steps of which require fetching the smallest value in a collection at any given point of time. Values are assigned to variables but are constantly modified, making it impossible for you to remember all…

1206封裝電容在物料可靠性設計比較低

1206封裝電容在物料可靠性設計中是要盡力避免的&#xff0c;盡量選擇0805或1210。在現場中容易出現電容因斷裂而擊穿的情況。同時容易造成保險絲燒斷。轉載于:https://www.cnblogs.com/conglinlixian/p/10414877.html

Java開發中 Double 和 float 不能直接運算

不能直接運算 是因為計算機儲存浮點類型的數值使用指數和尾數來表示 這就意味著計算時會出現“精度缺失”的現象 為了解決這個問題 我們引入 java.math.BigDecimal類來進行精確計算。 具體如下&#xff1a; public class Arith { //加法運算 public static double add(dou…

Unity3D 場景與C# Control進行結合

楊航最近在自學Unity3D&#xff0c;打算使用這個時髦、流行、強大的游戲引擎開發一個三維業務展示系統&#xff0c;不過發現游戲的UI和業務系統的UI還是有一定的差別&#xff0c;很多的用戶還是比較習慣WinForm或者WPF中的UI形式&#xff0c;于是在網上搜了一下WinForm和Unity3…

數據質量提升_合作提高數據質量

數據質量提升Author Vlad Ri?cu?ia is joined for this article by co-authors Wayne Yim and Ayyappan Balasubramanian.作者 Vlad Ri?cu?ia 和合著者 Wayne Yim 和 Ayyappan Balasubramanian 共同撰寫了這篇文章 。 為什么要數據質量&#xff1f; (Why data quality?) …

黑魔法(method-swizzling)解決第三方庫引發的問題

需求 最近做一個項目中&#xff0c;有個需求&#xff0c;所有網絡請求&#xff0c;都不顯示 NetworkActvityIndicator&#xff08;也就是狀態欄里旋轉的小圈圈&#xff09;. 解決過程1&#xff1a; 全局搜索 NetworkIndicator 關鍵字&#xff0c; 把所有涉及 NetworkIndicator …

Python 操作 MySQL 的5種方式(轉)

Python 操作 MySQL 的5種方式 不管你是做數據分析&#xff0c;還是網絡爬蟲&#xff0c;Web 開發、亦或是機器學習&#xff0c;你都離不開要和數據庫打交道&#xff0c;而 MySQL 又是最流行的一種數據庫&#xff0c;這篇文章介紹 Python 操作 MySQL 的5種方式&#xff0c;你可以…

unity3d 人員控制代碼

普通瀏覽復制代碼private var walkSpeed : float 1.0;private var gravity 100.0;private var moveDirection : Vector3 Vector3.zero;private var charController : CharacterController;function Start(){charController GetComponent(CharacterController);animation.w…

刪除wallet里面登機牌_登機牌丟失問題

刪除wallet里面登機牌On a sold-out flight, 100 people line up to board the plane. The first passenger in the line has lost his boarding pass but was allowed in regardless. He takes a random seat. Each subsequent passenger takes their assigned seat if availa…