django admin 或xdmin list_display search_fields list_filter 如果显示搜索外键或多对多字段

model

class UserProfile(AbstractUser):

    nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default="")
    gender = models.CharField(max_length=6, choices=(("male",u""),("female","")), default="female")
    address = models.CharField(max_length=100, default=u"")
    mobile = models.CharField(max_length=11, null=True, blank=True)
    add_time = models.DateTimeField(auto_now=True)


    class Meta:
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name

 

class Project(models.Model):
    project_name = models.CharField(max_length=200, verbose_name="项目名称", default="")
    project_number = models.CharField(max_length=200,verbose_name="项目编号", default="")
    project_user = models.ForeignKey(UserProfile,verbose_name='项目负责人')
    add_time = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "项目信息"
        verbose_name_plural = verbose_name


class Center(models.Model):
    center_name = models.CharField(max_length=200, verbose_name="中心名称", default="")
    center_number = models.CharField(max_length=200,verbose_name="中心编号", default="")
    center_user = models.ManyToManyField(UserProfile, verbose_name='中心负责人')
    add_time = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "中心信息"
        verbose_name_plural = verbose_name

 

admin或xadmin的展示

class CenterAdmin(object):
    """
    中心列表
    """

    list_display = ['id','center_name','center_number','center_user']  #后台自定义显示列

    search_fields = ['id','center_name','center_number','center_user__username'] #定义后台搜索 外键或多对多加__双下划线对应的字段名

    list_filter = ['id','center_name','center_number','center_user__username'] #通过时间搜索外键或多对多加__双下划线对应的字段名


class PatientAdmin(object):
    """
    患者列表
    """

    list_display = ['id','patient_number','patient_user']  #后台自定义显示列

    search_fields = ['id','patient_number','patient_user__username'] #定义后台搜索 外键或多对多加__双下划线对应的字段名索

    list_filter = ['id','patient_number','patient_user__username'] #通过时间搜索外键或多对多加__双下划线对应的字段名

 

posted on 2018-02-23 10:52  星河赵  阅读(825)  评论(0编辑  收藏  举报

导航