安装xadmin
pip install xadmin2
我的环境是Python3.6.8+Django2.2.4,装xadmin会出错- 在settings.py中启用
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'xadmin',
'crispy_forms',
]
全局配置
在adminx.py内添加:
import xadmin
from xadmin import views
#Register your models here.
class GlobalSettings(object):
site_title = "这是title"
site_footer = "这是footer"
menu_style = "accordion" #下拉式菜单
xadmin.site.register(views.CommAdminView, GlobalSettings)
在apps.py内添加verbose_name
属性:
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
verbose_name = "用户"
在_init_.py内添加:
default_app_config = "users.apps.UsersConfig"
效果如下:
后台管理model
- 在
models.py
设计model
from django.db import models
class Typeparent(models.Model):
type = models.CharField(max_length=50, verbose_name="类别")
name = models.CharField(max_length=50, verbose_name="名称", blank=True, null=True)
provider = models.ForeignKey(Provider, on_delete="CASCADE", verbose_name="供方")
#blank=True?;null=True:可为空
class Meta:
verbose_name = "型号"
verbose_name_plural = verbose_name
def __str__(self):
return self.type
- 所属app文件夹下新建adminx.py,注册model
import xadmin
from .models import Provider,Product
#Register your models here.
class ProviderAdmin(object):
pass
class ProductAdmin(object):
list_display = ['name', 'type', 'provider'] #配置显示列表
search_fields = ['name', 'type', 'provider'] #配置搜索框
list_filter = ['provider'] #配置过滤器
list_editable = ['name'] #可快速编辑
ordering = ['name'] #设置默认排序
readonly_fields = ['type'] #设置只读
exclude = ['provider'] #隐藏条目
xadmin.site.register(Provider, ProviderAdmin)
xadmin.site.register(Product, ProductAdmin)
外键搜索式录入
适用于外键数据量很大时的信息录入。方法为在外键的Admin配置中加入:
relfield_style = 'fk-ajax'
修改条目的图标
xadmin使用的是Font Awesome,文件路径为static\xadmin\vendor\font-awesome
,可以用最新的版本直接替换掉旧版本文件。在Admin配置中加入:
model_icon = 'fa fa-product-hunt' #名称为自己想用的图标,可以在官网上查看