I have a django model that I which to subclass that has a generic relationship attached that I wish to subclass:
class Person(models.Model):
name = models.CharField(max_length=255)
contact_details = generic.GenericRelation('ContactDetail')
class Teacher(Person):
field_of_study = models.CharField(max_length=255,default="Underwater Basket-weaving")
class ContactDetail(models.Model):
content_type = models.ForeignKey(ContentType, blank=True, null=True)
object_id = models.CharField(blank=True, null=True, max_length=256)
content_object = generic.GenericForeignKey('content_type', 'object_id')
value = models.CharField(max_length=256)
For clarity, I have to use a generic relation as department can also have contact details.
class Department(models.Model):
contact_details = generic.GenericRelation('ContactDetail')
When I create a person, I can make and get their contact details like so:
> alice = Person(name="Alice")
> ContactDetail(content_object=alice,value="555-1000")
> print alice.contact_details
[ ... list of things ... ]
However, when I make a teacher this happens:
> bob = Teacher(name="Bob")
> ContactDetail(content_object=bob,value="555-2000")
> print bob.contact_details
[ ... list of things ... ]
> bob_person = Person.get(name="Bob") # Returns the person instance of Bob
> print bob_person.contact_details
[]
Nothing is returned!
What I want is for a teachers contact details to be stored against the Person
objects and not the Teacher
objects. How can I do this?