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.mzph.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…