Django background improvement
List page display content modification
In the last blog, the list data display was realized, but only one column was displayed, and the column title was English. This blog first solved this problem.
Modify admin. In the blog folder Py file, the code is as follows:
from django.contrib import admin from blog.models import Blog # Register your models here. @admin.register(Blog) class BlogAdmin(admin.ModelAdmin): # list_display is used to set which fields are displayed on the list page list_display = ["title","content","creatr_time"]
list_ The list items corresponding to the value of the display attribute are all models The contents in the PY code should correspond well, otherwise the following similar errors will occur:
NameError: name 'creatr_time' is not defined
Execute the saved code, Django will automatically load it, and refresh the browser to see the effect.
After saving the code, reload the input data as follows:
D:\blog\admin.py changed, reloading. Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). March 25, 2021 - 12:04:53
List custom column implementation
Except models The attributes defined in py can be found in the list_ Call display, and you can also create your own columns for its use.
Modify models first Contents in py:
from django.db import models # Create your models here. class Blog(models.Model): title = models.CharField("title", max_length=100) content = models.TextField("content") creatr_time = models.DateField("Release time") def __str__(self): return self.title def introduction(self): return self.content[0:18]+"..." introduction.short_description = "content validity"
Synchronously modify admin Py, in the list_ New introduction in display:
@admin.register(Blog) class BlogAdmin(admin.ModelAdmin): # list_display is used to set which fields are displayed on the list page list_display = ["title","introduction","content","creatr_time"] search_fields = ["title"]
Run the code, and the effect diagram is as follows:
List search implementation
To achieve list retrieval, the code that needs to be added is also very simple, in admin Add the following code to the class BlogAdmin in. Py:
@admin.register(Blog) class BlogAdmin(admin.ModelAdmin): # list_display is used to set which fields are displayed on the list page list_display = ["title","content","creatr_time"] # Add search field search_fields = ["title"]
Read only field
In Django, you can set some fields not to be modified and read-only. For example, we set the publishing time as a read-only field.
Modify admin Code in py file:
@admin.register(Blog) class BlogAdmin(admin.ModelAdmin): # list_display is used to set which fields are displayed on the list page list_display = ["title","introduction","content","creatr_time"] search_fields = ["title"] readonly_fields = ["title"]
Common properties and methods of ModelAdmin
This blog mainly introduces the class BlogAdmin(admin.ModelAdmin), which inherits from ModelAdmin.
Common attributes are as follows
- list_display: list display field filtering;
- search_fields: search field filtering;
- readonly_fields: set read-only fields;
- ordering: sorting field;
- Fields: fields displayed on the form page;
- exclude: no fields are displayed on the form page;
- list_filter: sidebar filter;
- list_editable: editable field settings on the list page;
- list_per_page: the number of items displayed on the list page;
- empty_value_display: replace text with empty data.
Common methods are as follows
- save_model(): save;
- delete_model(): delete;
- get_search_results(): user defined retrieval;
- get_list_display(): List_ Extension of display attribute;
- get_readonly_fields(): for readonly_ Extension of fields attribute;
- get_search_fields(): Search_ Extension of fields attribute;
The above properties and methods will be learned in subsequent blogs. In addition to the above contents, Django has more QuerySet skills, which are left to later articles.