django-localflavor fields not showing up in Django admin models?

491 Views Asked by At

I am trying to implement django-localflavors into my Django app.

I import USStateSelect & USZipCodeField at the beginning of my models.py and then include them as a field in my model along with other fields, like so:

from localflavor.us.forms import USStateSelect, USZipCodeField
...
Class MyModel(models.Model):
... 
     state = USStateSelect()
     zip_5 = USZipCodeField()

However, when I go to Django admin and try to create a new Model object, I see every other field I wrote (CharFields, etc.) EXCEPT any of the localflavor fields. They are simply not showing up at all as an input field in my Model object form. I have done migrations on my database so that is not the issue.

Am I misunderstanding how to use django-localflavor? I read in an answer to a different post that localflavor doesn't actually create input fields, only stores data... but then I've also read that it DOES let you input data. At this point I am confused. Any help would be appreciated!

2

There are 2 best solutions below

0
On

I think what you are looking for are the model fields. The form fields are used when building your own forms (usually outside the admin, such as a contact form). Localflavor has a couple fields that should do what you need. Note that these are essentially CharFields that have some extra validation to make sure the follow the desired format.

0
On

You need to specify choices option.

Change your code a little as below:

from localflavor.us.forms import USStateSelect, USZipCodeField
...
Class MyModel(models.Model):
... 
     state = USStateSelect(choices=STATE_CHOICES) # add choices
     zip_5 = USZipCodeField() # no change on this line