Reputation: 197
I have a model that I would like to use the choices= option for, but three levels deep.
class Doctor(models.Model):
...
zipcode = models.CharField(max_length=10, choices=AREAS, null=True, blank=True)
Within the "zipcode" dropdown in the admin, I would like the hierarchy to be:
Bronx
--Kingsbridge
----10463
----10471
--Fordham
----10458
----10467
----10468
Brooklyn
--Borough Park
----11204
etc.
Then, if I choose zip code 10463, the Doctor object will be associated with the Kingsbridge area in the Bronx. I'm trying this a variety of different ways. The closest I've come is using this:
AREAS = (
('Bronx', (('Kingsbridge', ('10463', '10463'),),)),
...
)
Unfortunately, that gives me this hierarchy:
Bronx
--('10463', '10463')
which is weird and not helpful. Can anybody see where I'm going wrong? Is this hierarchy possible? Would it be smarter to just create another table in the app called Areas and use a manytomany field? The more I think about it, the more I think I have to use a manytomany field. Thanks in advance
Upvotes: 5
Views: 3910
Reputation: 7750
As per https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.choices, it seems to be only 2 level hierarchy is supported. If you need more than 2 levels, you need to use either custom widgets (or) multiple fields with Foreign Key relationships.
Upvotes: 1