How to limit choice field options based on another choice field in django admin
Posted
by umnik700
on Stack Overflow
See other posts from Stack Overflow
or by umnik700
Published on 2009-04-28T06:28:14Z
Indexed on
2010/05/12
19:34 UTC
Read the original article
Hit count: 325
I have the following models:
class Category(models.Model):
name = models.CharField(max_length=40)
class Item(models.Model):
name = models.CharField(max_length=40)
category = models.ForeignKey(Category)
class Demo(models.Model):
name = models.CharField(max_length=40)
category = models.ForeignKey(Category)
item = models.ForeignKey(Item)
In the admin interface when creating a new Demo, after user picks category from the dropdown, I would like to limit the number of choices in the "items" drop-down. If user selects another category then the item choices should update accordingly. I would like to limit item choices right on the client, before it even hits the form validation on the server. This is for usability, because the list of items could be 1000+ being able to narrow it down by category would help to make it more manageable.
Is there a "django-way" of doing it or is custom JavaScript the only option here?
© Stack Overflow or respective owner