【问题标题】:django-admin display the name field of a foreign key instead of foreign key iddjango-admin 显示外键的名称字段而不是外键 id
【发布时间】:2020-08-19 05:21:13
【问题描述】:

在我的 django-admin 中,它显示的是外键 id 值而不是外键的名称值。如何显示实际名称值。我为我的外键模型尝试了 str 方法,但没有改变任何东西。

class DemoPractice(models.Model):
    def __str__(self):
        return u'%s' % (self.PracticeName)
    Group = models.ForeignKey('DemoGroup', on_delete=models.CASCADE)
    PracticeName = models.CharField(max_length=255, null=True, blank=True)
    Primary = models.BooleanField(null=False, blank=False, default=True)
    ServiceAddressLine1 = models.CharField(max_length=255, help_text='New York', null=True, blank=True)
    ServiceAddressLine2 = models.CharField(max_length=255, help_text='New York', null=True, blank=True)
    ServiceAddressCity = models.CharField(max_length=255, help_text='New York', null=True, blank=True)
    ServiceAddressState = models.CharField(max_length=255, help_text='New York', null=True, blank=True)
    ServiceAddressZip = models.CharField(max_length=255, help_text='New York', null=True, blank=True)
    ServiceAddressCounty = models.CharField(max_length=255, help_text='10007', null=True, blank=True)
    OfficePhone = PhoneField(blank=True, help_text='1234567890 - 10 digits')
    OfficeFax = PhoneField(blank=True, help_text='1234567890 - 10 digits')
    OfficeEmail = PhoneField(blank=True, help_text='1234567890 - 10 digits')

【问题讨论】:

  • 请分享练习模型。
  • 完成:)。谢谢!
  • def __str__(self): return self.PracticeName 不起作用?
  • 我认为您应该尝试将 str 方法放在字段之后而不是它们之前
  • 没有区别。是的不工作

标签: django django-admin


【解决方案1】:

问题是您需要删除所有其他特殊格式字段(例如 filter_horizo​​ntal)并仅使用 raw_id_field。

【讨论】:

    猜你喜欢
    • 2019-06-26
    • 1970-01-01
    • 2015-05-08
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 2012-07-05
    相关资源
    最近更新 更多