Considering the class model as follows:
alt text http://www.forteresse.net/site/stackoverflow/classes.png/image
How do you do this in models.py?
class House(models.Model):
foo = models.CharField(max_length=123)
class Caravan(models.Model):
foo = models.CharField(max_length=123)
class Door(models.Model):
bar = models.CharField(max_length=123)
house = models.ForeignKey(House)
caravan = models.ForeignKey(Caravan)
But these foreign key definitions may not be what is intended. How do you code this in django? The intention is to reuse the same model “Door” for both “House” and “Caravan”.
After digging deeper, I found this; is this the right way to model the problem?
class House(models.Model):
foo = models.CharField(max_length=123)
class Caravan(models.Model):
foo = models.CharField(max_length=123)
class Door(models.Model):
bar = models.CharField(max_length=123)
house = models.ForeignKey(House, null=True, blank=True)
caravan = models.ForeignKey(Caravan, null=True, blank=True)
1 Answer