1.xadmin配置 setting.py

INSTALLED_APPS  =  (
    ...

    'xadmin' ,
    'crispy_forms' ,
    'reversion' ,

    ... 
)

2.注册类 adminx.py

import xadmin
from .models import Goods, GoodsImage


class GoodsAdmin(object):
    # 显示的字段
    list_display = ["name", "click_num", "sold_num", "fav_num", "goods_num", "market_price",
                    "shop_price", "goods_brief", "goods_desc", "is_new", "is_hot", "add_time"]
    # 提供筛选字段 (字段不能包含外键)
    search_fields = ['name', ]
    # 可以直接编辑的字段
    list_editable = ["is_hot", ]
    # 过滤
    list_filter = ["name", "click_num", "sold_num", "fav_num", "goods_num", "market_price",
                   "shop_price", "is_new", "is_hot", "add_time", "category__name"]
    style_fields = {"goods_desc": "ueditor"}
    # 图标
    model_icon = 'fa fa-book'
    # 倒序
    ordering = ['-click_nums']
    # 只读字段,不能编辑
    readonly_fields = ['click_nums']
    # 不显示的字段
    exclude = ['fav_nums']
    # 导出文件格式
    list_export = ('xls', 'xml', 'json')
    # 默认刷新时间
    refresh_times = (3, 5)
    # 可以通过url查找
    free_query_filter = True
    # 表
    data_charts = {
        "user_count": {'title(标题)': u"User Report", "x-field(x轴数据)": "date",
                       "y-field(y轴数据)": ("user_count", "view_count"),
                       "order": ('date',)},
        "avg_count": {'title(标题)': u"Avg Report", "x-field(x轴数据)": "date", "y-field(y轴数据)": ('avg_count',),
                      "order(排序)": ('date',)}
    }

    class GoodsImagesInline(object):
        model = GoodsImage
        exclude = ["add_time"]
        extra = 1
        style = 'tab'
  
  class odsImagesInline(object):
    model = sImages
    extra = 0
  # 连接 关联时间的多张表
  inlines = [GoodsImagesInline]
  # 当外键指向时候ajax加载数据
  ralfield_style = 'fk_ajax
  # 外键显示字段
  def __str__(self):
    return self.character

    


# 注册商品类
xadmin.site.register(Goods, GoodsAdmin)

  

3.配置后台主题 adminx.py

import xadmin
from xadmin import views

from .models import Law

# 配置后台主题
class BaseSetting(object):
    enable_themes = True
    use_bootswatch = True


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

4.配置后台系统名称和页脚版权、菜单样式 adminx.py

import xadmin
from xadmin import views

from .models import Law


# 后台系统名称页脚设置、设置后台菜单为收缩样式
class GlobalSetting(object):
    # 头部名称
    site_title = 'KCBT'
    site_footer = 'KCBT'
   # 设置收缩
    menu_style = 'accordion'


xadmin.site.register(views.CommAdminView, GlobalSetting)

  

5.设置app的中文名称 apps.py

from django.apps import AppConfig


class ZcfgConfig(AppConfig):
  name = 'zcfg'
  verbose_name = '政策法规'

更改 init .py文件

default_app_config = 'zcfg.apps.ZcfgConfig'

 

6.设置url

import xadmin

urlpatterns = [
    path('xadmin/', xadmin.site.urls),
]

  

7.设置图标

下载最新的图标库拷贝两个目录

替换

代码