First of all, some links to pages I've used for reference: A SO question, and the Django docs on generic relations and multi-table inheritance.
So far, I have a multi-table inheritance design set up. Objects (e.g: Car, Dog, Computer) can inherit an Item class. I need to be able to retrieve Items from the DB, get the subclass, and do stuff with it. My design doesn't allow for retrieving the different kinds of objects one by one, so I need to use the Item container to wrap them all into one. Once I have the Item, the Django docs say I can get the subclass by referencing the attribute with the name of the model (e.g: myitem.car or myitem.computer).
I don't know which type of object my item is referencing, so how can I get the child? Is there a built in way to do this? Here are some other ideas that I had: (some crazier than others)
- I was thinking I could add some sort of GenericForeignKey to Item that references the child, but I doubt it is even legal for a parent class to relate via a ForeignKey to a child class.
- I suppose I could have a ForeignKey(ContentType) in the Item class, and find the attribute of Item to get the child based on the ContentType's name.
- Finally, although an ugly method, I might be able to keep a list of object types, and try each as an attribute until a DoesNotExist error is not thrown.
As you can see, these proposed solutions are not that elegant, but I'm hoping I won't have to use one of them and someone here might have a better suggestion.
Thanks in advance