Django-用值替换“模型对象”

时间:2019-10-11 08:31:51

标签: django model admin

我在下面定义了一个货币模型:

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”列与其自身链接

在我的管理员中(下图),我可以看到链接,但是如果我打开下拉列表,则该标签不是用户友好的“货币对象(0)等...”

enter image description here

我可以使用“ Primary_Currency”的值“ Currency_Name”吗?

感谢您的帮助:)

1 个答案:

答案 0 :(得分:2)

使用模型类的__str__()方法,

DT[, 
   last_value2 := 
     fifelse(condition == "Yes", value, NA_integer_) %>% 
       shift() %>% 
       nafill("locf"),
   by = group
   ]