Django - admin creation and use

1. Introduction to Django Admin

Django Admin is a management backend tool that comes with the Django framework. It allows developers to easily manage the data models in the application through an intuitive web interface. Admin provides CRUD (Create, Read, Update, Delete) operations for models, as well as batch processing and search functions for data, greatly simplifying daily database management. It is not only suitable for the development stage, but also very suitable for data maintenance in the production environment.

2. Admin interface customization

Django Admin provides a wealth of customization options, allowing developers to adjust the interface according to project requirements.

2.2 Advanced Configuration Using the Admin Class

Django Admin uses the Admin class to configure the administrative interface of your models.

  • Create Admin Class : In admin.py, create an Admin class for each model, eg class MyModelAdmin(admin.ModelAdmin): ....
  • Register Admin class : Use admin.site.register(MyModel, MyModelAdmin)the registration model and its corresponding Admin class.
  • Configuration options : In the Admin class, you can set various options, such as list_displayspecifying the fields displayed on the list page, search_fieldsadding a search box, etc.

2.3 Field and Fieldset Management

  • Field managementfields : You can use the or attributes in the Admin class fieldsetsto control the display of fields in the edit page.
  • Fieldsets : fieldsets
    Allows you to group fields together, providing a better user experience, fieldsets = [('基本信息', {'fields': ['name', 'description']}), ...]e.g.

2.4 Optimization of list pages

  • Customize list views : Use list_displayto control which fields are displayed on the list page, list_filterto add filters, and orderingto set the default sort order.
  • Add action buttons : Use actionsproperties to add bulk actions, such as bulk delete or mark as read.

2.5 Using Inline Models

Inline models allow editing of related models directly within the parent model's edit page.

  • Define models inline : Use the attribute in the Admin class inlines
    , for example class RelatedModelInline(admin.TabularInline): model = RelatedModel.
  • Use inline model : Add inline model in Admin class of parent model, like inlines = [RelatedModelInline].

3 admin customization and extension

3.1 Customizing the Admin Template

  • Custom Admin Templates : Django Admin allows you to customize the appearance and behavior of the Admin interface, including modifying the appearance of model lists, forms, and detail pages.
  • Custom Admin Style : You can adjust the style of the Admin interface by overriding the Admin template or using custom CSS.

3.2 Using Admin actions

  • Admin actions : Django Admin provides the Admin actions feature, which allows you to batch process objects on the Admin interface, such as batch deletion, export, and other operations.
  • Custom Admin actions : You can write custom Admin actions to perform specific bulk operations.

3.3 Integrate third-party applications

  • Third-party application integration : Django allows you to easily integrate third-party applications. By installing and configuring third-party applications, you can extend the functionality of Django.

3.4 Internationalization and Localization

  • Internationalization : Django provides internationalization support, allowing you to localize your application into multiple languages.
  • Localization : By using Django’s built-in internationalization tools and translation mechanism, you can easily localize your application into different languages ​​and regions.

3.5 API development using Django REST Framework

  • Django REST Framework : DRF is a powerful and flexible tool for building Web APIs. It provides rich functionality, including serialization, views, authentication, permissions, and more.
  • API development : With DRF, you can quickly build an API that conforms to RESTful design principles and provide data interfaces for mobile applications, front-end frameworks, or other services.

Example of code customization:

# Register your models here.
from django.db import models

from blog.models import Article

admin.site.site_header= "自动化测试平台"
admin.site.site_title= "自动化测试平台"

#@admin.register(Article)

class ArticleAdmin(admin.ModelAdmin):
    #编辑要显示的信息
    list_display = ["title","brief_content","publish_date"]
    #list_filter = ['title']
    #搜索的字段
    search_fields = ['title', 'brief_content']


admin.site.register(Article,ArticleAdmin)

Guess you like

Origin blog.csdn.net/fish_study_csdn/article/details/142619913