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

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


This should work:

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

Here you're basically just copying over the values from the auth_user version into the extended_user one, and re-saving it. Not very elegant, but it works.


I found this answer by asking on django-user mailing list:

https://groups.google.com/d/msg/django-users/02t83cuEbeg/JnPkriW-omQJ

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").parentbar = Bar(parent=parent, happy_hour=True)bar.save_base(raw=True)

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


If you don't like __dict__.update solution you can do this:

for field in parent_obj._meta.fields    setattr(child_obj, field.attname, getattr(parent_obj, field.attname))