xadmin后台的建表与admin一样,只需在admin.py同级,新建一个adminx.py文件名不能为改为别的
adminx.py文件内容如下
import xadmin
from NewModel import models
class MoreInfo(object):
model = models.Userdetail
class ControUsers(object):
list_display = ('name', 'age')
list_per_page = 10
inlines = [MoreInfo]
xadmin.site.register(models.Users,ControUsers)
class ControArticle(object):
#list_display不能显示多对多,因此作者这里我们选择使用函数输出
list_display = ('title', 'zuozhe', 'create_time','update_time')
ordering = ('id',)
list_per_page = 10
filter_horizontal = ('auth',) #只显示作者,不显示别的用户
search_fields = ("title","body")
# list_display_links = ('title','auth',)
# list_editable = ('body',)
# list_filter = ('auth',)
# date_hierarchy = 'create_time' #按创建时间过滤选择
def zuozhe(self,obj):
return [a.name for a in obj.auth.all() ]
xadmin.site.register(models.Article,ControArticle)
class ControBank(object):
list_display = ('bank_name', 'city','point')
list_per_page = 10
xadmin.site.register(models.Bank,ControBank)
class ControBankcard(object):
list_display = ('cardid', 'possessor','info')
list_per_page = 10
raw_id_fields = ('info',)#只显示所属银行
xadmin.site.register(models.Bankcard,ControBankcard)
class ControUserdetail(object):
list_display = ('username', 'tel','city')
list_per_page = 10
xadmin.site.register(models.Userdetail,ControUserdetail)
区别在于:
- import xadmin
- 之前继承的是admin.ModelAdmin,这里继承的是object
- 之前inlines继承的admin.StackedInline,这里继承的是object