Django model inheritance: create sub-instance of existing instance (downcast)?

776 views
Skip to first unread message

guettli

unread,
May 12, 2015, 9:10:11 AM5/12/15
to django...@googlegroups.com
Hi,

this ticket is seven years old

https://code.djangoproject.com/ticket/7623

{{{

As it exists now, multi-table inheritance does not allow for the creation of a child model instance that inherits from an existing parent model instance. For example:

Parent Class-

class Place(models.Model):
    name = models.CharField(max_length=50)
    address = models.TextField(max_length=150)

Child Classes-

class Restaurant(Place):
    place = models.OneToOneField(Place, parent_link=True)
    cuisine = models.CharField(max_length=75)
    rating = models.IntegerField()

class Bar(Place):
    parent = models.OneToOneField(Place, parent_link=True)
    happy_hour = models.BooleanField()
    beers_on_tap = models.ManyToManyField("Beers", null=True, blank=True)

Sample Use-case-

When the system is first deployed, a restaurant instance is created. Later, the restaurant adds a bar to increase revenue, and we now want to create a Bar model instance for the parent Place for the restaurant. I would propose the following interface for doing so:

parentPlace = Restaurant.objects.get(name__iexact="Bob's Place").parent
barInstance = Bar(parent=parentPlace, happy_hour=True)
However, if you attempt to create an instance in this manner now, you receive a DatabaseIntegrityError, saying that a Place object with that id already exists.

}}}

How to get this solved?

There are work arounds:

http://stackoverflow.com/questions/4064808/django-model-inheritance-create-sub-instance-of-existing-instance-downcast

{{{
extended_user = ExtendedUser(user_ptr_id=auth_user.pk)
extended_user.__dict__.update(auth_user.__dict__)
extended_user.save()
}}}

Or is there a better solution in current django versions?






 

Mario Gudelj

unread,
May 12, 2015, 11:41:44 PM5/12/15
to django...@googlegroups.com
Have you had a look at http://django-model-utils.readthedocs.org/en/latest/managers.html#inheritancemanager? I'm not sure if it will help but it's kind of related to what you've posted.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d4273754-670e-4697-b0b4-9626c42a5aaf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

guettli

unread,
May 13, 2015, 5:25:50 AM5/13/15
to django...@googlegroups.com
Thank you for this link. The InheritanceManager is good to know about. But it does not help creating the child_instance from a base instance. At least the docs don't tell about it. Or I was blind.

Simon Charette

unread,
May 13, 2015, 10:43:36 AM5/13/15
to django...@googlegroups.com
Hi Guettli,

This isn't part of the public API but you could rely on how Django loads fixture internally.

parent = Restaurant.objects.get(name__iexact="Bob's Place").parent
bar
= Bar(parent=parent, happy_hour=True)
bar
.save_base(raw=True)

Keep in mind that this could break with any new version of Django.

Simon

guettli

unread,
May 18, 2015, 2:22:39 AM5/18/15
to django...@googlegroups.com
Thank you Simon. I did not know save_base(raw=True) before.
Reply all
Reply to author
Forward
0 new messages