How to limit ForeignKey field to another model FK depending on value

65 views
Skip to first unread message

Ahmad Zoughbi

unread,
Feb 1, 2016, 2:14:59 PM2/1/16
to Django users
I have the following models in Django, and using smart-selects:

class Country(models.Model):
    name
= models.CharField(max_length=100)


class Province(models.Model):
    name
= models.CharField(max_length=100)
    country
= models.ForeignKey(Country)


class City(models.Model):
    name
= models.CharField(max_length=100)
    country
= models.ForeignKey(Country)
    province
= models.ForeignKey(Province)


In the fixtures, i added multiple countries, with their provinces, and cities.

I'm using smart-selects for chaining in this model

class WorkArea(models.Model):
    work_area
= models.CharField(max_length=100)
    country
= models.ForeignKey(Country)
    province
=  ChainedForeignKey(Province, chained_field="country",chained_model_field="country")
    city
= ChainedForeignKey(City, chained_field=province", chained_model_field="province")


Now i have this model:

class Project(models.Model):
    project_name
= models.CharField(max_length=100)
    province
= models.ForeignKey(Province)


The question: In the model Project how do i show only provinces from Province model, that has country set to X (If i have countries "USA" and "Canada" i want the field province to show list of provinces in "USA" only/preselecting country).

James Schneider

unread,
Feb 1, 2016, 5:22:20 PM2/1/16
to django...@googlegroups.com
The question: In the model Project how do i show only provinces from Province model, that has country set to X (If i have countries "USA" and "Canada" i want the field province to show list of provinces in "USA" only/preselecting country).


You would filter the choices available to the field in your form with a query like this:

Province.objects.filter(country__name="X") # where X is the country you want.

I'm not familiar with smart-selects, so you would need to look at their documentation to figure out where that filter goes, but it is probably part of the 'choices' keyword argument for your form field.

If you plan to do this though, you may want to add unique=True to the name field on Country (unless you plan on having multiple countries with the same name, of course). I believe this also adds an index, which will help speed up your searches. If you can't do unique=True, at least set db_index=True.

-James

Ahmad Zoughbi

unread,
Feb 2, 2016, 4:20:32 AM2/2/16
to Django users
Thanks James. 
But i think this solution will apply also on both models (WorkArea and Project) right? while i want to get it to work only on the field province of model Project . 

Ahmad Zoughbi

unread,
Feb 2, 2016, 4:03:27 PM2/2/16
to Django users
Just for reference: 
i could get it to work by passing limit_choices_to to the "province" in Project model. 

class Project(models.Model):
    project_name 
= models.CharField(max_length=100)

    province 
= models.ForeignKey(Province, limit_choices_to={"country": 1})
Reply all
Reply to author
Forward
0 new messages