admin back-end database management

admin back-end database management

django provide a fairly complete interface to back-end database management, process calls for the development and test use

django model class will gather all the registered levied for data management interface for these model classes, for developers who use

Steps for usage:

  1. Create an Account Admin:$ python3 manage.py createsuperuser

  Follow the prompts to complete the registration, refer to the following:

Python3 manage.py createsuperuser $ 
the Username (the Leave blank to use ' tarena ' ): tarena   # here to enter a user name 
Email address: [email protected]   # here to enter the mailbox 
Password: # Enter the password here (password is more complicated, otherwise it will prompt for password is too simple) 
password (again): # re-enter duplicate password 
Superuser created successfully.

   2. using a registered account login admin interface

    Admin login Address: http://127.0.0.1:8000/admin

A custom back-end data management table

To define model classes themselves are also able to  /admin display a background in management and community management, their class needs to be registered with the admin interface

Add your own background management table definition of the model class, you need admin.site.register(自定义模型类) to register method

Configuring the following steps:

1. Import model models class registration applications to be managed in the app in admin.py

  from . import models

2. Call admin.site.register method to register

  from django.contrib import admin

  admin.site.register (custom model class)

Such as: the bookstore / admin.py add the following code to manage class Book

# File: child of the bookstore / admin.py 
from django.contrib Import ADMIN
 # the Register your Models here Wallpaper. 

From . Import Models 
... 
admin.site.register (models.Book)   # The Book class registration manageable page

 

Second, modify the background to show the form of Models

Manage the admin database data records are shown as custom  XXXX object type of record, to read from and Analyzing

In the model of user-defined classes can override  def __str__(self): methods to resolve display problems

  Such as: rewriting in a custom model class  STR (Self) method returns the displayed text:

class Book(models.Model):
    ...
    def __str__(self):
        return "书名" + self.title

 

Third, the model manager class

Role: to add new features for ease of operation admin interface.

Description: Manage class must be inherited from  django.contrib.admin inside the  ModelAdmin class

Model Manager to use:

1. In the  <应用app>/admin.py definition of model manager class

class XXXX_Manager(admin.ModelAdmin):
    ......

 

2. Register with the model class association manager

from django.contrib Import ADMIN
 from . Import Models 
admin.site.register (models.YYYY, XXXX_Manager) 
# registration models.YYYY model classes associated with the management class XXXX_Manager

 

Example:

# file : bookstore/admin.py
from django.contrib import admin
from . import models

class BookAdmin(admin.ModelAdmin):
    list_display = ['id', 'title', 'price', 'market_price']

admin.site.register(models.Book, BookAdmin)

 

Enter http://127.0.0.1:8000/admin/bookstore/book/  view the display and different from the past

Model Manager class ModelAdmin implemented advanced management capabilities

  1. list_display to control which fields are displayed in the list of changes in the Admin page.
  2. list_display_links can control list_display in the column should be linked to the object "Change" page.
  3. list_filter Set Active Admin to modify the list of filters in the right column of page
  4. search_fields setting is enabled Admin change the search box on the list page.
  5. list_editable set list of field names on the model, which will allow for editing on the change list page.
  6. Other See https://docs.djangoproject.com/en/1.11/ref/contrib/admin/

Fourth, the database table management

 1. Modify the model class field displays the name

  • The first parameter model class each field is verbose_name, this field shows the name will be displayed in the background database management page
  • By verbose_name field options, modify the display name

Examples are as follows:

= title models.CharField ( 
    MAX_LENGTH = 30 , 
    the verbose_name = ' Display Name ' 
)

 

2. Define the properties and forms of presentation by Meta inner class model class

  Model classes can be redefined and the current attributes data model class definition of internal information table class class Meta

Use the following format:

class Book (models.Model): 
    title = as CharField (....)
     class Meta -:
         1. named db_table, = ' table name ' 
            - the name used by the model data table. (Need to update the synchronization immediately after setting database)
         2. the verbose_name = ' singular name ' 
            - a name easily understood (odd) to an object model for display / ADMIN management interface
         3. verbose_name_plural = ' complex name ' 
            - plural form of the object name (s), for displaying / admin management interface

 

Example:

class Meta: 
    db_table = ' book_table '   
# original table name "bookstore_book" replaced "book_table", please view the data sheet verbose_name = ' booooook ' verbose_name_plural = ' booksssssss '
# to 127.0.0.1:8000/admin look at where changed?

 

Guess you like

Origin www.cnblogs.com/maplethefox/p/11235414.html