rfkid
rfkid

Reputation: 37

How do I change the rendering properties of a django choice field?

The below code is rendering 'US' or 'CA' to the template according to a user's response. How do I make it render the corresponding 'United States' or 'Canada' value instead two character code?

From: models.py

CANADA = 'CA'
UNITED_STATES = 'US'
COUNTRY_CHOICES = [
    (CANADA, 'Canada'),
    (UNITED_STATES, 'United States'),
]
address_country = models.CharField(
    max_length=2,
    choices=COUNTRY_CHOICES,
    default=UNITED_STATES,
    null=True,
)

From: forms.py

class AddressForm(forms.ModelForm):
    class Meta:
        model = Address
        fields = ['address_country',]
        labels = {'address_country': 'Country',}

Template

Country: {{address.address_country}}

Upvotes: 1

Views: 211

Answers (1)

Pruthvi Barot
Pruthvi Barot

Reputation: 2018

try this in template

{{address.get_address_country_display}}

Upvotes: 2

Related Questions