0

I have the following models:

class Person(models.Model):
    # fields

class Teacher(Person):
    # fields

class Student(Person):
    # fields
    teacher = models.ForeignKey(teacher)

class Staff(Person):
    # fields

class SomeModel(models.Model):
    # fields
    point_person = models.ForeignKey(Person)

But I want to limit my "point_person" to Teacher and Student only. How can I do this?

Joyce
  • 15
  • 7

1 Answers1

0

I would offer to implement a custom manager, probably overriding the get_queryset. I can see 2 solutions to get only those Parent, who have ChildA and/or ChildB.

  • If one Parent never has ChildA and ChildB same time (or any other combination), you can add an extra field (db column) to Parent, which indicates what is the class of its child object, if any. In get_queryset of your custom manager you always check this field.
  • If one Parent can have multiple classes of Child simultaneously, of if you don't want to add an extra column, then you override get_queryset, to actually select from ChildA and ChildB, and combine the querysets afterwards into a single queryset.
Community
  • 1
  • 1
Art
  • 2,235
  • 18
  • 34
  • Child B is related to ChildA, if that helps? I'll edit the post – Joyce Oct 22 '15 at 08:46
  • Okay I assume you're not using neither abstract nor proxy in the model meta. And you have separate tables for Parant, ChildA, ChildB and ChildC. Therefore there is an implicit one to one relation between ClindA and Parent, ChildB and Parent, ChildC and Parent. You want to make a ForeignKey to Parent, that will only select those Parent, who have ChildA and ChildB. I am going to edit the answer anyways. – Art Oct 22 '15 at 09:04