Code

Opened 5 months ago

Closed 5 months ago

#21537 closed New feature (duplicate)

Add a bit more flexibility to the multi-table model inheritance

Reported by: ivanbright@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.5
Severity: Normal Keywords: model inheritance multi-table
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Thank you.

Attachments (0)

Change History (1)

comment:1 Changed 5 months ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Thanks for the report. This problem is tracked in #7623.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.