Python---The use of ORM in Django (3) admin configuration and use

Source: Internet
Author: User
Tags myadmin

Create a new project and open

8080

Visit Admin Page

http://127.0.0.1:8080/admin

There is no account and password at this time: you need to configure the database before generating the user

Metabase python manage.py makemigrationspython manage.py migrate create user python manage.py createsuperuser need to fill in user name, email, password

Manage the Django Database app---version >phpmyadmin,web management database

To create a data table:

#-*-coding:utf-8-*- from__future__ Import unicode_literals fromdjango.db Import models# Create your models here.classPublisher (models. Model): Name= Models. Charfield (max_length= -, verbose_name="name") Address= Models. Charfield ("Address", max_length= -) City= Models. Charfield ('City', max_length= -) State_province= Models. Charfield (max_length= -) Country= Models. Charfield (max_length= -) website=models. Urlfield ()classMeta:verbose_name='Publishers'verbose_name_plural=verbose_name def __str__ (self):returnSelf.nameclassAuthor (models. Model): Name= Models. Charfield (max_length= -) def __str__ (self):returnSelf.nameclassAuthordetail (models. Model): Sex= Models. Booleanfield (max_length=1, choices= (0,'male'), (1,'female')) e -Mail=models. Emailfield () address= Models. Charfield (max_length= -) Birthday=models. Datefield () Author=models. Onetoonefield (Author)classBook (models. Model): Title= Models. Charfield (max_length= -) Authors=models. Manytomanyfield (Author) Publisher=models. ForeignKey (Publisher) publication_date=models. Datefield () Price= Models. Decimalfield (max_digits=5, decimal_places=2,default=Ten) def __str__ (self):returnSelf.title
models.py
Python manage.py Makemigrationspython manage.py Migrate
Configuration Database

Configuring the database for Management in admin.py

 from __future__ Import unicode_literals  from django.contrib Import Admin  from app01.models import *# Register your Models Here.admin.site.register (book) Admin.site.register ( Publisher)   #会显示设置的verbose_name The display name of the field in admin
Admin.site.register (Author)
8080
Start Project

Visit again

To add to a Web page:

If there is a Chinese error, the solution to see: Python---Supplemental Django Chinese error

If you want to set the page to Chinese display, you can set the settings file:

' en -US '  'zh-hans'

Since only title is returned in __str__ when an Orm object is created, the page displays only the book name, and the other information is not fully

class Book (models. Model):    = models. Charfield (max_length=)    = models. Manytomanyfield (Author)    = models. ForeignKey (Publisher)    = models. Datefield ()    = models. Decimalfield (max_digits=5, decimal_places=2Default=ten)     def __str__ (self):  #__str__需要返回字符串
Return Self.title

When no __str__ is present, only the

To display more information, we need to customize the class in the Admin.py module to set the displayed field

class myadmin (admin. Modeladmin):    = ("title","price","  Publisher")    #设置显示的字段, unrelated to the original __str__ Admin.site.register (book,myadmin)   #使MyAdmin与Book产生联系admin.site.register (Publisher) admin.site.register (Author)

You can modify the fields in the models and set the alias to appear in admin

class Book (models. Model):    = models. Charfield (max_length=,verbose_name= "title")

Search box:

class myadmin (admin. Modeladmin):    = ("title","price","  Publisher")    #设置显示的字段 is irrelevant to the    original __str__ = ("title" ,"price",)      #会生成搜索框, a field in the tuple that is allowed to search

Filter:

classmyadmin (admin. Modeladmin): List_display= ("title"," Price","Publisher"#设置显示的字段, it has nothing to do with the original __str__ search_fields= ("title"," Price",) #会生成搜索框, a field in the tuple that is allowed to search List_filter= (" Price", "publisher") #生成过滤器, Filter by Price

Sort:

classmyadmin (admin. Modeladmin): List_display= ("title"," Price","Publisher"#设置显示的字段, it has nothing to do with the original __str__ search_fields= ("title"," Price",) #会生成搜索框, a field in the tuple that is allowed to search List_filter= (" Price","Publisher") #过滤器 Ordering= ("-price",) #排序, default ID sort, ascending, descending, use before field'-'

Show and hide: use when adding and modifying:

    FieldSets = [        (None, {'fields': ['title')  ]}),    #None代表其他字段隐藏, field in fields display    ]

FieldSets = [        (None, {'fields': ['title']}) ,        ('price information', {'fields': [  ' Price'"publisher"],}),  #组名为 the price Information
]

FieldSets =[None, {' Fields': ['title']}),        ('Price Information', {' Fields': [' Price',"Publisher"],'Classes': ['collapse']}), #classes样式折叠]

Python---The use of ORM in Django (3) admin configuration and use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.