Admin--django comes with database management tools

Source: Internet
Author: User

Admin is a Django-brought database management tool, an app

In admin.py, you can customize the class to make the Admin Database Administration page show the information you want to see

models.py

 fromDjango.dbImportModels#Create your models here.classBook (models. Model): Title= Models. Charfield (max_length=32) Price= Models. Decimalfield (max_digits=6, decimal_places=2) Pub_date=models. Datefield () Authors= Models. Manytomanyfield (to="Author")    def __str__(self):returnSelf.titleclassAuthor (models. Model): Name= Models. Charfield (max_length=32)    def __str__(self):returnSelf.name
View Code

admin.py

 fromDjango.contribImportAdmin fromApp01.modelsImport*#Register your models here.classbookconfig (admin. Modeladmin):"""Custom classes enable you to see the information you want to see on the Admin Database Administration page"""List_display= ["title"," Price","pub_date"]#Many-to-many fields cannot be used hereList_display_links = ["title"]#after setting the linked field settings, click the field to go to the edit pageList_filter = ["pub_date"]#record a query with the field you set as a filterList_editable = [" Price"]#set editable fields, note: If you set a field in List_display_links, you can no longer set it here .Search_fields = ["title"," Price"]#Set the search field (Fuzzy query: Enter a keyword to query)Date_hierarchy ="pub_date"  #Filter Date    #action: Batch operation record    defFunc (self, request, Queryset):#Request: Requested Queryset: Selected data that you want to manipulate        Print(self, request, Queryset)#to manipulate a selected record:Queryset.update (pub_date="2012-1-1")#change the publication date of the selected record to January 1, 2012Func.short_description ="publication date changed to January 1, 2012"Actions=[func,] fields= ["title"," Price","pub_date","authors"]#fields displayed on the page where the record is added    #exclude = ["Pub_date"] # field not displayed on the page where the record is added, contrary to fieldsOrdering= ["ID"]#Sort Descending by ID in ascending order ["-id"]admin.site.register (book, bookconfig)#Source: Register (self, model_or_iterable, Admin_class=none, **options)#Model_or_iterable=book, Admin_class=bookconfigPrint(admin.site._registry) admin.site.register (Author)
View Code

Admin--django comes with database management tools

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.