I noticed that there is no class us.models.USZipCodeField in
django.contrib.localflavor.us, although there is a model for phone
numbers. The django guide for submitting patches says to discuss
alternatives here first, so I wanted to do just that. I cobbled
together some code by copying the phone number code and modifying it.
I discovered some buggy behaviour at first with max_length, so I ended
up modifying the form as well by removing the argument for it, since I
saw no purpose in it. This brings it into line with the other forms,
none of whom specify max length. I removed min length while I was at
it, for consistency.
The new form is identical to the old form, only with max and min
length removed:
class USZipCodeField(RegexField):
default_error_messages = {
'invalid': _('Enter a zip code in the format X or X-
.'),
}
def __init__(self, *args, **kwargs):
super(USZipCodeField, self).__init__(r'^\d{5}(?:-\d{4})?$',
*args, **kwargs)
The new model is extremely similar to the telephone number model:
class ZipCodeField(CharField):
description = _("Zip code")
def __init__(self, *args, **kwargs):
kwargs['max_length'] = 10
super(ZipCodeField, self).__init__(*args, **kwargs)
def formfield(self, **kwargs):
from django.contrib.localflavor.us.forms import USZipCodeField
defaults = {'form_class': USZipCodeField}
defaults.update(kwargs)
return super(ZipCodeField, self).formfield(**defaults)
Thoughts? Concerns? Should this be modified? It seems to work just
fine for me.
--
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en.