middleofdreams
middleofdreams

Reputation: 353

limit choices for foreign field based on other field

Is it possible to do really dynamic form in AdminModel? I have following models:

class MyModel(models.Model):
    firstfield=models.ForeignKey(First)
    secondField= models.ForeignKey(Second, blank=True,null=True)
    #some other fields

class Second(models.Model):
    firstfield=models.ForeignKey(First)
    #other fields

As you can see Second is optional. But I want it to limit according to current selection in First? It would require some page refreshing or some ajax work but I simply don't know how to even pass First value. Maybe I should add it to request and then use something similar to: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey ?

Upvotes: 4

Views: 2563

Answers (1)

Ahsan
Ahsan

Reputation: 11832

You can do it through ajax request. If you don't know how it works see the below links.

  1. How to implement two dropdowns dependent on each other using Django and jQuery
  2. Dynamic select fields with JQuery and django

Upvotes: 2

Related Questions