Django中字段选择的“人类可读”值

时间:2017-12-19 07:20:43

标签: django model choicefield

PAYCODE_BLUEPAY = "BLUEPAY"
PAYCODE_HEARTLAND = "HEARTLAND"
PAYCODE_1STPAY = "1STPAYMENT"
PAYCODE_CHOICES =(
    (PAYCODE_1STPAY, '1St Payment'),
    (PAYCODE_BLUEPAY, 'Bluepay Payment'),
    (PAYCODE_HEARTLAND, 'HeartLand Payment'),

)

class Payment(models.Model):
    paymentmethod = models.CharField("Payment Method", max_length=20, choices = PAYCODE_CHOICES, blank=False, null=False)

    def __str__(self):
        return self.paymentmethod 

方法__str__(self)将返回BLUEPAYHEARTLAND1STPAYMENT。但我希望__str__(self)返回第一笔付款 Bluepay付款 HeartLand付款

请给我解决方案。感谢。

1 个答案:

答案 0 :(得分:10)

def __str__(self):
    return self.get_paymentmethod_display()

请参阅get_FOO_display()