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

1

6
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.

Tout answered 19/12, 2017 at 7:20 Comment(0)
B
20
def __str__(self):
    return self.get_paymentmethod_display()

see get_FOO_display()

Bridgman answered 19/12, 2017 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.