Re: Multi-table inheritance - knowing the child object from the parent
http://docs.djangoproject.com/en/1.2/topics/db/models/#id7 You can access sub-classes with place.restaurant and place.gardenstore if the model has them. I did something similar the other day but added an extra field to parent class to indicate which type of a child model it had (1 for Restaurant etc). I don't know if this is a proper way to handle the situation but it works for me. -Jori On May 18, 7:11 pm, Lee Hinde wrote: > On Tue, May 18, 2010 at 4:27 AM, Jani Tiainen wrote: > > > Given the following: > > > > class Place(models.Model): > > > name = models.CharField(max_length=50) > > > address = models.CharField(max_length=80) > > > zip_code = models.CharField(max_length=15) > > > > class Restaurant(Place): > > > serves_hot_dogs = models.BooleanField() > > > serves_pizza = models.BooleanField() > > > > class GardenStore(Place): > > > sells_lady_bugs = models.BooleanField() > > > > and given a query on zip_code in Places, how would I know whether a > > > Place is a Restaurant or a Garden Store? > > > You don't OOTB. > > > I asked same thing quite a while ago - you should be able to find my > > thoughts > > somewhere in this group archives. > > > Rationale behind this is that you're trying to achieve something pretty > > much > > impossible - inheritance in relational world. > > > Thing is that you can have you can really have both: Restaurant and > > GardenStore instance to point to same place - so is that correct case or > > not? > > > Note that "inheritance" is made so that you have relation from Restaurant > > and > > GardenStore tables to Place. There is no simple mechanism to prevent your > > data > > to have references from both "children" to point to "parent". > > > You can have property/method on your model that tries to determine which > > one > > is correct. > > > Or you can re-factor your models (which is usually less painful) so that > > your > > place, provided that it is unique (which actually in this case might not > > be). > > Add simple discriminator field to tell what type of child you must also > > fetch > > to make data complete. > > Thanks Jani; I'd seen some of the older threads, with similar work arounds, > but those discussions pre-dated recent advances. I'll look again for the > thread you mention. > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group > athttp://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Multi-table inheritance - knowing the child object from the parent
On Tue, May 18, 2010 at 4:27 AM, Jani Tiainen wrote: > > Given the following: > > > > class Place(models.Model): > > name = models.CharField(max_length=50) > > address = models.CharField(max_length=80) > > zip_code = models.CharField(max_length=15) > > > > > > class Restaurant(Place): > > serves_hot_dogs = models.BooleanField() > > serves_pizza = models.BooleanField() > > > > class GardenStore(Place): > > sells_lady_bugs = models.BooleanField() > > > > > > and given a query on zip_code in Places, how would I know whether a > > Place is a Restaurant or a Garden Store? > > You don't OOTB. > > I asked same thing quite a while ago - you should be able to find my > thoughts > somewhere in this group archives. > > Rationale behind this is that you're trying to achieve something pretty > much > impossible - inheritance in relational world. > > Thing is that you can have you can really have both: Restaurant and > GardenStore instance to point to same place - so is that correct case or > not? > > Note that "inheritance" is made so that you have relation from Restaurant > and > GardenStore tables to Place. There is no simple mechanism to prevent your > data > to have references from both "children" to point to "parent". > > You can have property/method on your model that tries to determine which > one > is correct. > > Or you can re-factor your models (which is usually less painful) so that > your > place, provided that it is unique (which actually in this case might not > be). > Add simple discriminator field to tell what type of child you must also > fetch > to make data complete. > > > Thanks Jani; I'd seen some of the older threads, with similar work arounds, but those discussions pre-dated recent advances. I'll look again for the thread you mention. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Multi-table inheritance - knowing the child object from the parent
> Given the following: > > class Place(models.Model): > name = models.CharField(max_length=50) > address = models.CharField(max_length=80) > zip_code = models.CharField(max_length=15) > > > class Restaurant(Place): > serves_hot_dogs = models.BooleanField() > serves_pizza = models.BooleanField() > > class GardenStore(Place): > sells_lady_bugs = models.BooleanField() > > > and given a query on zip_code in Places, how would I know whether a > Place is a Restaurant or a Garden Store? You don't OOTB. I asked same thing quite a while ago - you should be able to find my thoughts somewhere in this group archives. Rationale behind this is that you're trying to achieve something pretty much impossible - inheritance in relational world. Thing is that you can have you can really have both: Restaurant and GardenStore instance to point to same place - so is that correct case or not? Note that "inheritance" is made so that you have relation from Restaurant and GardenStore tables to Place. There is no simple mechanism to prevent your data to have references from both "children" to point to "parent". You can have property/method on your model that tries to determine which one is correct. Or you can re-factor your models (which is usually less painful) so that your place, provided that it is unique (which actually in this case might not be). Add simple discriminator field to tell what type of child you must also fetch to make data complete. -- Jani Tiainen -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.