Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The “human-readable” value of the field choices in Django

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 

The method __str__(self) will return BLUEPAY or HEARTLAND or 1STPAYMENT. But I want __str__(self) return 1St Payment, Bluepay Payment or HeartLand Payment.

Please give me solution. Thanks.

like image 863
tnductam Avatar asked Dec 02 '22 12:12

tnductam


1 Answers

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

see get_FOO_display()

like image 166
Ykh Avatar answered Dec 16 '22 10:12

Ykh