外键指定显示的字段
来源:4-8 organization modesl.py设计
大副大
2018-10-14
机构里面关联了城市的外键,在机构的表里面生成的是city_id
class CourseOrg(models.Model):
name = models.CharField(max_length=50, verbose_name=u"机构名称")
desc = models.TextField(verbose_name=u"机构描述")
tag = models.CharField(default="全国知名", max_length=10, verbose_name=u"机构标签")
category = models.CharField(default="pxjg", verbose_name=u"机构类别", max_length=20,
choices=(("pxjg", "培训机构"), ("gr", "个人"), ("gx", "高校")))
click_nums = models.IntegerField(default=0, verbose_name=u"点击数")
fav_nums = models.IntegerField(default=0, verbose_name=u"收藏数")
image = models.ImageField(upload_to="org/%Y/%m", verbose_name=u"logo", max_length=100, null=True, blank=True)
address = models.CharField(max_length=150, verbose_name=u"机构地址")
city = models.ForeignKey(CityDict, on_delete=models.CASCADE, verbose_name=u"所在城市")
students = models.IntegerField(default=0, verbose_name=u"学习人数")
course_nums = models.IntegerField(default=0, verbose_name=u"课程数")
add_time = models.DateTimeField(default=datetime.now)
为什么xadmin里面显示的是城市的名字,如果我想显示城市的其他字段,比如desc,那怎么改写呢
class CityDict(models.Model):
desc = models.CharField(max_length=200, verbose_name=u"描述")
name = models.CharField(max_length=20, verbose_name=u"城市")
add_time = models.DateTimeField(default=datetime.now)
class Meta:
verbose_name = u"城市"
verbose_name_plural = verbose_name
def __str__(self):
return self.name
写回答
1回答
-
bobby
2018-10-17
你可以定义方法 在方法中去获取外键中的字段,然后将这个方法名配置到list_display中
00
相似问题