0

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?

DjangoGuy
  • 103
  • 2
  • 13

1 Answers1

2

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')
vZ10
  • 2,468
  • 2
  • 23
  • 33
  • isn't there a way to do it using ForeignKey? – DjangoGuy Jun 12 '17 at 15:15
  • In your schema, one Group can have refer to one School and one Manger. But as I understand there could be more than one Magnaer per School. – vZ10 Jun 12 '17 at 15:38
  • What if there would be one manager per school? How can I do it? – DjangoGuy Jun 12 '17 at 15:40
  • In that case, The most simple way is to make for loop in template through all the Managers and show managers only from that school. – vZ10 Jun 12 '17 at 15:49