Working with Django LocalFlavor Zip as a Model Field


Working with Django LocalFlavor Zip as a Model Field



What's the best way to implement the following:

### models.py >>> from django.db import models >>> from django.contrib.auth.models import User # Create the client class. >>> class Client(models.Model): ...     user = models.OntToOneField(User) ...     zip = ***???***()  ### forms.py >>> from django.forms import ModelForm # Create the form class. >>> class ArticleForm(ModelForm): ...     class Meta: ...         model = Client 

I'm trying to get the end result to use the US Zip Codes Field form widget/validator using only modifications the models.py file... aka I don't want to do the following:

### forms.py >>> from django.forms import ModelForm >>> from django.contrib.localflavor.us.forms import USZipCodeField # Create the form class. >>> class ArticleForm(ModelForm): ...     class Meta: ...         model = Client ...     zip = USZipCodeField() 

NOTE: It might be that the BEST way to accomplish it is in the forms.py file as shown above... If this is the case, I guess I'm missing the argument/benefit of having it there instead of as a custom model field (e.g., DRY?, loose coupling?) Any help on helping better understand the benefits of forms.py over models.py in this scenario would be greatly appreciated.

Thanks in advance! -Tom




Adding reports to Django's admin

1:



How can I include user profile's images/logos on django comments
A zip code is just a string of numbers/letters, so there's no need to define a custom field for storing it in the database.


What is the best practice to use ExtJS with Django Framework
There's no model-level validation in Django at the moment (although there's a Google Summer of Code project to add it), so a custom model field wouldn't add anything.


How can I use django forms/models to represent choices between fields?
The validation comes at the form level, which is why there's a custom form field..
Can I use a database view as a model in Django?


How to repeat a “block” in a django template


How do I implement a common interface for Django related object sets?

2:



Django: retrieving abstract-derived models
This must not have been available when you asked the question, but django 1.4 has django.contrib.localflavor.us.models.USPostalCodeField(), which you'd think would do what you need, but it is designed for storing those 2-letter state abbreviations, not Zip Codes, unfortunately.

.



91 out of 100 based on 41 user ratings 391 reviews