I. Django built-in ADMI
A.configuring a route
Urlpatterns = [ url (r'^admin/', Admin.site.urls), ]
B. Custom admin in the admin.py only need to talk about the mode of a class registration, you can implement additions and deletions in the admin function, such as:
Admin.site.register (models. UserInfo)
However, this is a simpler approach, and if you want to do more custom operations, you need to take advantage of modeladmin, such as:
Way One: class useradmin (admin. Modeladmin): = ('user'pwd',) # The first parameter can be a list
Mode two: @admin. Register (models. UserInfo) # The first parameter can be a list of class Useradmin (admin. Modeladmin): list_display = (' user ', ' pwd ',)
A number of customizable features are available in the 3.ModelAdmin, such as:
1. List_display, when listing, customize the displayed columns.
Class Useradmin (admin. Modeladmin): list_display = (' user ', ' pwd ', ' xxxxx ') def xxxxx (self, obj): return "xxxxx"
2. List_display_links, when listing, customize the column can click Jump.
Class Useradmin (admin. Modeladmin): list_display = (' user ', ' pwd ', ' xxxxx ') list_display_links = (' pwd ',)
3. List_filter, when listing, customize the right quick filter.
Class Useradmin (admin. Modeladmin): list_display = (' user ', ' pwd ') class Ugg (admin. Simplelistfilter): title = _ (' decade born ') parameter_name = ' xxxxxx ' List_filter = (' user ', Ugg,)
4. list_select_related, list, even if the query is automatically select_related
5. Paging related
# pagination, number of bars per page list_per_page = 100 # pagination, show all (real data < This value will only show All) List_max_show_all = 200 # page plug-in paginator = Pag Inator
6. List_editable, List of columns that can be edited
Class Useradmin (admin. Modeladmin): list_display = (' user ', ' pwd ', ' ug ',) list_editable = (' ug ',)
7. Search_fields, the function of fuzzy search when list
Class Useradmin (admin. Modeladmin): search_fields = (' user ', ' pwd '
8. Date_hierarchy, when listing, search for date and datetime types
Class Useradmin (admin. Modeladmin): date_hierarchy = ' CTime '
9. Preserve_filters, detailed page, delete, modify, update after jumping back to the list, whether to retain the original search criteria
Save_as = False, Detail page, button "Sava as new" or "Sava and add another"
Save_as_continue = True, click Save and Continue editing
Save_as_continue = true # if Save_as=true,save_as_continue = True, click the Sava as New button to continue editing. # if save_as=true,save_as_continue = False, click the Sava as New button to return to the list.
Save_on_top = False, Detail page, whether the Save Delete button is also displayed at the top of the page
Inlines, detailed page, if there are other tables and the current table to do FK, then the detailed page can be dynamically added and deleted
Class Userinfoinline (admin. Stackedinline): # tabularinline extra = 0 model = models. UserInfo class Groupadminmode (admin. Modeladmin): List_display = (' id ', ' title ',) inlines = [Userinfoinline,]
Actions in the action, list, custom action
Class Useradmin (admin. Modeladmin): # Customizing Action behavior specific Method def func (self, request, Queryset): print (self, request, Queryset) Print (Request. Post.getlist (' _selected_action ')) func.short_description = "Chinese display custom Actions" actions = [Func,] # The action option is displayed at the top of the page actions_on_top = True # action option is displayed at the bottom of the page actions_on_bottom = False # Whether to display the number of selections Actions_selection_counter = True
15. Customizing HTML templates
Add_form_template = Nonechange_form_template = Nonechange_list_template = Nonedelete_confirmation_template = Nonedelete_selected_confirmation_template = Noneobject_history_template = None
Raw_id_fields, Detail page, to the FK and the-out fields in the form input box
Class Useradmin (admin. Modeladmin): raw_id_fields = (' FK field ', ' field ', ')
. fields, when the detail page is displayed, the fields of the field
Class Useradmin (admin. Modeladmin): Fields = (' user ',)
Exclude, the excluded fields when the detail page
Class Useradmin (admin. Modeladmin): exclude = (' user ',)
Readonly_fields, read-only fields when detailed pages
Class Useradmin (admin. Modeladmin): readonly_fields = (' user ',
FieldSets, detailed pages, using fieldsets tags to split the display of data
Class Useradmin (admin. Modeladmin): fieldsets = (' basic data ', { ' Fields ': (' user ', ' pwd ', ' ctime ', ') }), (' other ', { ') Classes ': (' collapse ', ' wide ', ' Extrapretty '), # ' collapse ', ' wide ', ' Extrapretty ' ' Fields ': (' user ', ' pwd '), }), )
21. When the detailed page is displayed, the data movement is selected (direction: up and down and around)
Class Useradmin (admin. Modeladmin): filter_vertical = ("field",) # or Filter_horizontal = ("field",)
Ordering, list, data collation
Class Useradmin (admin. Modeladmin): ordering = ('-id ',) or def get_ordering (self, request): return ['-id ',]
View_on_site, whether the view on set is displayed on the page when editing
View_on_site = False or Def view_on_site (self, obj): return ' https://www.baidu.com '
Radio_fields, using the radio display option when using the Detail page (FK defaults to select)
Radio_fields = {"UG": admin. VERTICAL} # or admin. Horizontal
Show_full_result_count = True, the number of data styles that are displayed after a fuzzy search in the list
Class Useradmin (admin. Modeladmin): # show_full_result_count = True # 1 result (total) # show_full_result_count = False # 1 result (Show All) Search_fields = (' user ',)
Python's Django Admin summary