I have a two models that are similar, but not exactly the same. Here's the best abstraction of the problem that I can come up with.
class Cat(models.Model):
name = models.TextField()
breed = models.TextField()
class Dog(models.Model):
name = models.TextField()
color = models.TextField()
And now I need to make another model like this.
class Pet(models.Model):
favoriteFood = models.TextField()
isCat = models.BooleanField()
animal = models.ForeignKey(?????????)
My problem is that the animal field of the Pet model is going to be a foreign key to either the Cat or the Dog model depending on the value of isCat. How can I do that?
Now, I know this is an unusual/awkward schema in the first place, but I wasn't involved in its creation and I can't change it. I just have to support it. I'm writing these models for an existing database.
Generic relations is a direct answer.
You should see Generic relations.
Another option for this use case is: django-polymorphic :)