5

Not sure, but I think this may be a bug?

Here is my model:

class Property(models.Model):
Name = models.CharField(max_length=40)
Description = models.TextField(default="Description Not Available")
Address = models.CharField(max_length=60, default="Not Available")
Address2 = models.CharField(max_length=60,null=True)
City = models.CharField(max_length=60, null=True)
State = usa_model.USStateField(null=True)
Code = usa_model.USPostalCodeField(null=True)
Phone = usa_model.PhoneNumberField(null=True)

enter image description here

Am I missing something?

-Kerry

Kerry Hatcher
  • 601
  • 6
  • 17

1 Answers1

4

Perhaps you are looking for the US Zip Code Form Field?

The USPostalCodeField uses a list of the states + a few extras:

COFA_STATES = (
    ('FM', 'Federated States of Micronesia'),
    ('MH', 'Marshall Islands'),
    ('PW', 'Palau'),
)

At django.contrib.localflavor.us.us_states

# USStateField
STATE_CHOICES = tuple(sorted(US_STATES + US_TERRITORIES + ARMED_FORCES_STATES, key=lambda obj: obj[1]))

# USPostalCodeField
USPS_CHOICES = tuple(sorted(US_STATES + US_TERRITORIES + ARMED_FORCES_STATES + COFA_STATES, key=lambda obj: obj[1]))
Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • Ah I see. So what should my model be? integer max_length=5? or CharField max=10? Also how would I add the Form Field to the admin side? Thanks! – Kerry Hatcher Apr 25 '11 at 18:50
  • 3
    `CharField`, since validation accepts XXXX-XXXX. For the admin, just specify a ModelForm with one field: `Code = ...USZipCodeField` and override `ModelAdmin.form` with your ModelForm - should be like 6 lines of code :) – Yuji 'Tomita' Tomita Apr 25 '11 at 20:39
  • Then if you are using ModelForm, it will just use the text field without any validation. So how do you make it validate correctly when using ModelAdmin? – miki725 Jun 27 '12 at 18:34
  • @miki725: read my comment above, define your form then pass it to the ModelAdmin form attribute. – Yuji 'Tomita' Tomita Jun 27 '12 at 19:59
  • sorry, did not see that. thanx – miki725 Jun 27 '12 at 22:54