Django测试开发-27- xadmin模板中注册增加Student和Teacher,Card及CardDetail表

一、models.py新增

1.1 老师表(多对多)

class Teacher(models.Model):

    '''
    老师表
    '''

    teacher_name = models.CharField(max_length=30, verbose_name="老师", default="")
    mobile = models.CharField(max_length=30, verbose_name="电话", default="")
    mail = models.CharField(max_length=30, verbose_name="邮箱", default="")

    class Meta:

        verbose_name = "老师"
        verbose_name_plural = verbose_name

    def __str__(self):

        return self.teacher_name

 

1.2 学生成绩表(多对多)

class Student(models.Model):

    """学生表"""
    student_no = models.CharField(max_length=10,verbose_name="学号")
    student_name = models.CharField(max_length=30,verbose_name="姓名")
    student_age = models.IntegerField(verbose_name="年龄")
    student_score = models.IntegerField(verbose_name="分数")
    student_gender_select = (("M",""),("W",""))        # 性别做成为下拉选择框样式
    student_gender = models.CharField(max_length=10,
                                      choices=student_gender_select, # 设置性别选项
                                      verbose_name="性别",
                                      default=""
                                      )

    # 多对多
    teachers = models.ManyToManyField(Teacher,verbose_name="老师")


    class Meta:
        verbose_name = "学生成绩"
        verbose_name_plural = verbose_name


    # def __str__(self):
    #
    #     return self.student_name

1.3 银行卡基本信息

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

1.4 银行卡详情

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

二、执行数据库表生成及迁移语句

python3 manage.py makemigrations

python3 manage.py migrate

数据库中会生成

 

 xadmin_student_teachers是因为使用了外键

三、adminx.py注册表

3.1 学生和老师

class ControlStudent(object):

    # 显示字段
    list_display = ('student_no','student_name','student_age','student_score','student_gender','老师')

    # 搜索条件
    search_fields = ('student_name',)

    # 每页显示n条
    list_per_page = 5
   # 关联老师表中的字段,然后显示在列表页
    def 老师(self,obj):
    
        return [x.teacher_name for x in obj.teachers.all()]





class ControlTeacher(object):

    list_display = ['teacher_name','mobile']

xadmin.site.register(Student,ControlStudent)
xadmin.site.register(Teacher,ControlTeacher)

3.2 银行卡及详情信息

class MoreInfo(object):

    model = CardDetail


class ControlCard(object):

    list_display = ['card_id','card_user','add_time','电话']

    # 在Card页面显示更多信息CardDetail
    inlines = [MoreInfo]

    def 电话(self,obj):

        return obj.carddetail.tel

# 注册card表,关联CardDetail
xadmin.site.register(Card,ControlCard)

 

posted @ 2020-03-31 12:25  旅行没有终点  阅读(212)  评论(0编辑  收藏  举报