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;通常我们使用鼠标来选…

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;并取名…

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资源。并且是无…

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…

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?) …

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…

字符串操作截取后面的字符串_对字符串的5个必知的熊猫操作

字符串操作截取后面的字符串We have to represent every bit of data in numerical values to be processed and analyzed by machine learning and deep learning models. However, strings do not usually come in a nice and clean format and require preprocessing to con…

最新 Unity3D鼠标滑轮控制物体放大缩小 [

var s 1.0;function Update () {var cube GameObject.Find("Cube");if(Input.GetAxis("Mouse ScrollWheel")){s Input.GetAxis("Mouse ScrollWheel");cube.transform.localScaleVector3(1*s,1*s,1*s);}}

sublime-text3 安装 emmet 插件

下载sublime&#xff0c;http://www.sublimetext.com/ 安装package control &#xff1a;https://packagecontrol.io/ins... 这个地址需要翻墙&#xff0c;访问不了的可以看下图 import urllib.request,os,hashlib; h 6f4c264a24d933ce70df5dedcf1dcaee ebe013ee18cced0ef93d…

unity3d]鼠标点击地面人物自动走动(也包含按键wasdspace控制)

目录(?)[-] 一效果图二大概步骤 创建一个plane设置层为Terrain因为后面要判断是否点击的是这个层准备好人物模型并且将三个脚本拖放到人物上并且将动画文件也拖放好记得看前面提醒哦 ThirdPersonCamera相当于smoothflowThirdPersonController修改版mouseMoveContr鼠标点击人物…

Web 开发基础

一、 Web 开发简介 最早的软件都是运行在大型机上的&#xff0c;软件使用者登陆到大型机上去运行软件。后来随着 PC 机的兴起&#xff0c;软件开始主要运行在桌面上&#xff0c;而数据库这样的软件运行在服务器端&#xff0c;这种 Client/Server 模式简称 CS 架构。随着互联网的…

power bi函数_在Power BI中的行上使用聚合函数

power bi函数Aggregate functions are one of the main building blocks in Power BI. Being used explicitly in measures, or implicitly defined by Power BI, there is no single Power BI report which doesn’t use some sort of aggregate functions.聚合功能是Power BI…

广义估计方程估计方法_广义估计方程简介

广义估计方程估计方法A key assumption underpinning generalized linear models (which linear regression is a type of) is the independence of observations. In longitudinal data this will simply not hold. Observations within an individual (between time points) …

Unity3d鼠标点击屏幕来控制人物的走动

今天呢&#xff0c;我们来一起实现一个在RPG中游戏中十分常见的功能&#xff0c;通过鼠标点击屏幕来控制人物的走动。首先来说一下原理&#xff0c;当我们点击屏幕时&#xff0c;我们按照一定的方法&#xff0c;将屏幕上的二维坐标转化为三维坐标&#xff0c;然后我们从摄像机位…