DRF — use of xadmin

Time:2021-8-20

Use of xadmin

1 Django has a built-in admin. Some people think it's ugly and not powerful enough. These people wrote a xadmin to replace admin
2 xadmin is an app of Django
3 how to use
    -Distinguish between 1. X and 2. X
        -1.x :pip3 install xadmin
        -2.x :pip3 install git+git://github.com/sshwsfc/[email protected]
               pip3 install https://codeload.github.com/sshwsfc/xadmin/zip/django2
    -Django2. X + xadmin 2. X environment setup
        -Installation
        -Register in app
            'xadmin',
            'crispy_forms',
            'reversion',
        -Modify internationalization
            #Modify and use Chinese interface
            LANGUAGE_CODE = 'zh-Hans'
            #Modify time zone
            TIME_ZONE = 'Asia/Shanghai'       USE_TZ = False
-Data migration
            python3 manage.py makemigrations
            python3 manage.py migrate
        -Configure routing information
            import xadmin
            xadmin.autodiscover()
            #The version module automatically registers models that require version control
            from xadmin.plugins import xversion
            xversion.register_models()

            urlpatterns = [
                path('xadmin/', xadmin.site.urls)
            ]
        -Create superuser
            python manage.py createsuperuser
        -Log in, access the address, enter the user name and password to enter
            http://127.0.0.1:8000/xadmin/
4 beautification
    -Write a class in admin.py
    class BookClass():
        #Fields displayed in this table
        list_display = ['id', 'name', 'price','publish']
        #Search by these fields
        search_fields = ['id','name']
        #By which fields
        list_filter = ['is_delete']

        #Data can be exported to excel, JSON and XML formats
        list_export = ('xls', 'xml', 'json')
        # list_ Set export to none to disable data export
        list_export_fields = ('id', 'name', 'price')
        data_charts = {
            "order_amount": {
                'title ':' write freely ',
                "x-field": "price",
                "y-field": ('publish',),
                "order": ('id',)
            },

        }
     -Register
    xadmin.site.register(models.Book,BookClass)

 

Amin.py Code:

import xadmin
from xadmin import views
from app01 import models
class BaseSetting():
    "" "basic configuration of xadmin" ""
    enable_ Themes = true # enables theme switching
    use_bootswatch = True

class GlobalSettings():
    "" "global configuration of xadmin" ""
    site_ Title = "Luffy learning city" # set the site title
    site_ Footer = "Luffy Learning City Co., Ltd." # set the footer of the site
    # menu_ Style = "accordion" # set menu collapse




xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)


class BookClass():
    #Fields displayed in this table
    list_display = ['id', 'name', 'price','publish']
    #Search by these fields
    search_fields = ['id','name']
    #By which fields
    list_filter = ['is_delete']

    #Data can be exported to excel, JSON and XML formats
    list_export = ('xls', 'xml', 'json')
    # list_ Set export to none to disable data export
    list_export_fields = ('id', 'name', 'price')
    data_charts = {
        "order_amount": {
            'title ':' write freely ',
            "x-field": "price",
            "y-field": ('publish',),
            "order": ('id',)
        },

    }

#This was registered in admin before, and now it has become xadmin
xadmin.site.register(models.Book,BookClass)
xadmin.site.register(models.Publish)
xadmin.site.register(models.AuthorDetail)
xadmin.site.register(models.Author)