All,

I've added a ticket for this issue:

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

If anyone has a chance to verify that this is a bug (rather than a programming 
error on my part) I would greatly appreciate it.

Thanks!
Dow


On Jul 18, 2013, at 11:08 PM, Dow Street <dowstr...@prodigy.net> wrote:

> All,
> 
> I'm seeing some unexpected behavior when using multi-level multi-table 
> inheritance.  In the example below there are three levels in the class 
> hierarchy.  When accessing the name field (which is stored in the Level1 base 
> class) from an object in Level3, the query does not seem to walk the 
> hierarchy correctly.
> 
> Note:  I am using explicitly defined id and OneToOneFields so that I can 
> specify the db_column names (for compatibility with other legacy software 
> using the same database).  Here's the models file, followed by a console log 
> where I created test objects (in an otherwise blank database):
> 
> 
> 
> # models.py
> 
> from django.db import models
> 
> class Level1(models.Model):
>  """ Top level base class """
> 
>  level1_id = models.AutoField(primary_key=True, db_column='Level1_ID')
>  name = models.CharField(max_length=32, db_column='Name', blank=True, 
> null=True)
> 
>  class Meta:
>      db_table = 'Level1'
> 
> 
> class Level2(Level1):
>  """ Middle level class """
> 
>  level2_id = models.AutoField(primary_key=True, db_column='Level2_ID')
>  level1 = models.OneToOneField('Level1', db_column='Level1_ID', 
> parent_link=True)  # parent class
> 
>  class Meta:
>      db_table = 'Level2'
> 
> 
> class Level3(Level2):
>  """ Bottom level class """
> 
>  level3_id = models.AutoField(primary_key=True, db_column='Level3_ID')
>  level2 = models.OneToOneField('Level2', db_column='Level2_ID', 
> parent_link=True)  # parent class
> 
>  class Meta:
>      db_table = 'Level3'
> 
> 
> 
> From the manage.py shell I add two objects to an otherwise empty database:
> 
> (InteractiveConsole)
> 
> from sc_test.models import *
> 
> # add Level1 object
> top1 = Level1()
> top1.name = 'Top 1'
> top1.save()
> 
> # add Level3 object
> bot1 = Level3()
> bot1.name = 'Bot 1'
> bot1.save()
> 
> # query for Level1 objects
> l1 = Level1.objects.all()
> l1
> [<Level1: Level1 object>, <Level1: Level1 object>]
> 
> # query for Level2 objects
> l2 = Level2.objects.all()
> l2
> [<Level2: Level2 object>]
> 
> # query for Level3 objects
> l3 = Level3.objects.all()
> l3
> [<Level3: Level3 object>]
> 
> # Get name of first Level1 object (looks ok)
> l1[0].name
> u'Top 1'
> 
> # Get name of second Level1 object (looks ok)
> l1[1].name
> u'Bot 1'
> 
> # First Level2 object (looks ok)
> l2[0].name
> u'Bot 1'
> 
> # First Level3 object (!! WRONG !!)
> l3[0].name
> u'Top 1'
> 
> (WRONG - expected this to return u'Bot 1')
> 
> Can anyone shed some light?  Specifically, does multi-table inheritance only 
> work with one level of subclassing?  If not, is there some syntax trick I'm 
> missing, or is this a bug in the inheritance code?
> 
> 
> Thanks,
> Dow

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to