Reputation: 149
I have defined a field in Django
zip_code = models.CharField(_('Zip code'), max_length=19, blank=True, null=True)
In the moment, I need that each letter to be capitalized for that field. I use this field in formlayout.py
file :
Row(
Column('city', css_class="s12 m5"),
Column('state', css_class="s12 m5"),
Column('zip_code', css_class="s12 m2"),
),
How could I capitalize zip_code
without overriding the field itself? Could I create a @property
method in which I will transform this field? What is the best place to put this method? utils.py
? views.py
? models.py
?
For example, if originally zip_code
is j3w 1w7, then I want it to become J3W 1W7
Upvotes: 1
Views: 1582
Reputation: 308919
Assuming you have a form or model form, the easiest thing to do is to add a clean_<fieldname>
method.
class MyForm(forms.ModelForm):
...
def clean_zip_code(self):
return self.cleaned_data['zip_code'].upper()
Upvotes: 3