Monday, February 1, 2016

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

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).

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/93b58ffe-5b8a-4f0e-ba12-ff6a3d408b05%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment