xadmin後臺若是要對錶的內容增刪改查,跟以前的admin.py文件裏面寫註冊表信息同樣,需在admin.py同一級目錄新建一個adminx.py的文件。
而後在adminx.py文件控制頁面上需顯示的內容。html
models模塊跟以前設計表是同樣的,在models.py文件設計表的字段顯示,以Student表爲例。python
# models.py from django.db import models # Create your models here. class Student(models.Model): '''學生成績''' student_id = models.CharField(max_length=30, verbose_name="學號") name = models.CharField(max_length=30, verbose_name="姓名") age = models.IntegerField(verbose_name="年齡") score = models.IntegerField(verbose_name="分數") class Meta: verbose_name = "學生成績" verbose_name_plural = verbose_name
若是有表的關聯,以以前的admin後臺一對一關係OneToOneField爲例http://www.javashuo.com/article/p-fpfbtzan-bw.htmldjango
# models.py class Card(models.Model): '''銀行卡 基本信息''' card_id = models.CharField(max_length=30, verbose_name="卡號", default="") card_user = models.CharField(max_length=10, verbose_name="姓名", default="") add_time = models.DateField(auto_now=True, verbose_name="添加時間") class Meta: verbose_name = "銀行卡帳戶_基本信息" verbose_name_plural = '銀行卡帳戶' def __str__(self): return self.card_id class CardDetail(models.Model): '''銀行卡詳情信息''' card = models.OneToOneField(Card, on_delete=models.CASCADE, verbose_name="卡號" ) tel = models.CharField(max_length=30, verbose_name="電話", default="") mail = models.CharField(max_length=30, verbose_name="郵箱", default="") city = models.CharField(max_length=10, verbose_name="城市", default="") address = models.CharField(max_length=30, verbose_name="詳細地址", default="") class Meta: verbose_name = "帳戶_我的資料" verbose_name_plural = verbose_name def __str__(self): return self.card.card_user
在admin.py文件的同一目錄新建一個adminx.py(注意只能是adminx.py,不能叫其它的名稱)
在adminx.py裏與以前的admin.py代碼有一些不同spa
# adminx.py import xadmin from .models import Student, Card, CardDetail class ControlStudent(object): # 顯示的字段 list_display = ('student_id', 'name', 'age', 'score') # 搜索條件 search_fields = ('name',) # 每頁顯示10條 list_per_page = 10 class MoreInfo(object): model = CardDetail class ControlCard(object): list_display = ["card_id", "card_user", "add_time"] # 在Card頁面顯示更多信息CardDetail inlines = [MoreInfo] # 註冊Student表 xadmin.site.register(Student, ControlStudent) # 註冊card表,關聯CardDetail xadmin.site.register(Card, ControlCard)
登陸xadmin後臺後頁面顯示設計
學生成績表編輯頁面code
銀行卡帳戶使用inlines關聯過來的頁面顯示htm