limit ForeignKey choices based on another ForeginKey field

424 Views Asked by At

Let's say I have an app's structure that looks like this :

** models.py **

Class School(models.Model):
    name = models.CharField(max_length=500)


Class Manager(models.Model)
    name = models.Charfield(max_length=500)
    school = models.ForignKey(School)


Class Group(models.Model)
    name = models.Charfield(max_length=500)
    school = models.ForeignKey(School)
    manager = models.ForeignKey(Manager, related_name="group_manager")

In the template I want users to be able to create groups (in the school page) and choose among managers who belong to the same school only!

Any idea?

1

There are 1 best solutions below

4
vZ10 On

If you have a table with 2 foreign keys and one name it probably should be a through table for ManyToMany relation between Shool and Manager.

Class School(models.Model):
    name = models.CharField(max_length=500)
    managers = models.ManyToMany(Manager, through='Group')