Add a bit more flexibility to the multi-table model inheritance
|Reported by:||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.5|
|Severity:||Normal||Keywords:||model inheritance multi-table|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Hi. I'm sorry if it's not a proper place for such an issue, but i've not found the another way to contact to you.
I suppose, it would be useful if Django would allow to create an instance of derived model based on the existing instance of the parent model.
Let's consider two simple models (Django v1.5.5 assumed), one inherited from another:
from django.db import models class StreetAddress(models.Model): street_name = models.CharField(max_length=64) building_number = models.PositiveSmallIntegerField() def __str__(self): return "%s %d" % (self.street_name, self.building_number) class Cafe(StreetAddress): name = models.CharField(max_length=64)
StreetAddress intentionally created as non-abstract, because i suppose its instances being created independently from the cafe instances or another possible descendants. Let's try to create some cafe located by some address:
Python 2.7.4 (default, Apr 19 2013, 18:28:01) [GCC 4.7.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from heir_demo.models import StreetAddress, Cafe >>> addr = StreetAddress.objects.create(street_name='Piccadilly', building_number=5) >>> addr <StreetAddress: Piccadilly 5> >>> mollys = Cafe.objects.create(streetaddress_ptr=addr, name='Mollys') Traceback (most recent call last): File "<console>", line 1, in <module> ... stacktrace goes here ... Warning: Column 'building_number' cannot be null
I posted it on http://stackoverflow.com/questions/20287111/the-want-of-consistency-in-django-models-inheritance/. Thus, if i just misunderstand something, please, post an explanation there.