Django - 用值替换 "model object"
Django - replace "model object" by the value
我有一个货币模型定义如下:
class Currency(models.Model):
"""
Currency Model
Defines the attribute of Currency
"""
class Meta:
verbose_name = "Currency"
verbose_name_plural = "Currencies"
ordering = ['Currency_Name']
def __str__(self):
return self.Currency_Name
Currency_Date = models.DateTimeField(auto_now_add=True)
Currency_Date_Update = models.DateTimeField(auto_now=True)
Currency_Name = models.CharField(max_length=3, unique=True)
Is_Secondary_Ccy = models.CharField(max_length=1, choices=Y_N_BOOLEAN)
Primary_Currency = models.ForeignKey('self', on_delete=models.DO_NOTHING, null=True) # to refer to itself
Primary_Factor = models.IntegerField(default=1)
Currency_Name_Reuters = models.CharField(max_length=3)
模型通过 "Primary_Currency"
列链接到自身
在我的管理员中(下图)我可以看到链接,但是如果我打开下拉菜单,标签对用户来说不友好"Currency object (0) etc..."
我可以得到 "Primary_Currency" 的值 "Currency_Name" 吗?
感谢您的帮助:)
使用模型 class、
的 __str__()
方法
class Currency(models.Model):
...
# your code
<b>def __str__(self):
try:
return self.Primary_Currency.Currency_Name
except AttributeError:
return self.Currency_Name</b>
我有一个货币模型定义如下:
class Currency(models.Model):
"""
Currency Model
Defines the attribute of Currency
"""
class Meta:
verbose_name = "Currency"
verbose_name_plural = "Currencies"
ordering = ['Currency_Name']
def __str__(self):
return self.Currency_Name
Currency_Date = models.DateTimeField(auto_now_add=True)
Currency_Date_Update = models.DateTimeField(auto_now=True)
Currency_Name = models.CharField(max_length=3, unique=True)
Is_Secondary_Ccy = models.CharField(max_length=1, choices=Y_N_BOOLEAN)
Primary_Currency = models.ForeignKey('self', on_delete=models.DO_NOTHING, null=True) # to refer to itself
Primary_Factor = models.IntegerField(default=1)
Currency_Name_Reuters = models.CharField(max_length=3)
模型通过 "Primary_Currency"
列链接到自身在我的管理员中(下图)我可以看到链接,但是如果我打开下拉菜单,标签对用户来说不友好"Currency object (0) etc..."
我可以得到 "Primary_Currency" 的值 "Currency_Name" 吗?
感谢您的帮助:)
使用模型 class、
的__str__()
方法
class Currency(models.Model):
...
# your code
<b>def __str__(self):
try:
return self.Primary_Currency.Currency_Name
except AttributeError:
return self.Currency_Name</b>