Fixed github issue 15 (query result incomplete with inheritance). Thanks to John Debs.

fix_request_path_info
Bert Constantin 2011-01-24 16:37:38 +01:00
parent 3cc7d483c1
commit 2c47db8fcc
5 changed files with 67 additions and 13 deletions

View File

@ -225,6 +225,17 @@ ul.auto-toc {
</div> </div>
<div class="section" id="changelog"> <div class="section" id="changelog">
<h1>Changelog</h1> <h1>Changelog</h1>
<div class="section" id="v1-0-release-candidate-1">
<h2>2011-01-24 V1.0 Release Candidate 1</h2>
<div class="section" id="bugfixes">
<h3>Bugfixes</h3>
<ul class="simple">
<li>Fixed GitHub issue 15 (query result incomplete with inheritance).
Thanks to John Debs for reporting and the test case.</li>
</ul>
</div>
</div>
<hr class="docutils" />
<div class="section" id="v1-0-beta-2"> <div class="section" id="v1-0-beta-2">
<h2>2010-11-11 V1.0 Beta 2</h2> <h2>2010-11-11 V1.0 Beta 2</h2>
<div class="section" id="this-is-a-v1-0-testing-release"> <div class="section" id="this-is-a-v1-0-testing-release">
@ -283,7 +294,7 @@ more simple and intuitive use:</p>
</li> </li>
</ul> </ul>
</div> </div>
<div class="section" id="bugfixes"> <div class="section" id="id1">
<h3>Bugfixes</h3> <h3>Bugfixes</h3>
<ul class="simple"> <ul class="simple">
<li>Custom fields could cause problems when used as the primary key. <li>Custom fields could cause problems when used as the primary key.
@ -349,7 +360,7 @@ transform the result to its polymorphic equivalent.</p>
</li> </li>
</ul> </ul>
</div> </div>
<div class="section" id="id1"> <div class="section" id="id2">
<h3>Bugfixes</h3> <h3>Bugfixes</h3>
<ul class="simple"> <ul class="simple">
<li>Removed requirement for primary key to be an IntegerField. <li>Removed requirement for primary key to be an IntegerField.
@ -387,7 +398,7 @@ just <tt class="docutils literal">python manage.py test</tt>.</p>
</div> </div>
</div> </div>
<hr class="docutils" /> <hr class="docutils" />
<div class="section" id="id2"> <div class="section" id="id3">
<h2>2010-2-22</h2> <h2>2010-2-22</h2>
<p>IMPORTANT: API Changed (import path changed), and Installation Note</p> <p>IMPORTANT: API Changed (import path changed), and Installation Note</p>
<p>The django_polymorphic source code has been restructured <p>The django_polymorphic source code has been restructured
@ -411,11 +422,11 @@ from polymorphic.models import PolymorphicModel, ...
<ul class="simple"> <ul class="simple">
<li>minor API addition: 'from polymorphic import VERSION, get_version'</li> <li>minor API addition: 'from polymorphic import VERSION, get_version'</li>
</ul> </ul>
<div class="section" id="id3"> <div class="section" id="id4">
<h3>New Features</h3> <h3>New Features</h3>
<p>Python 2.4 compatibility, contributed by Charles Leifer. Thanks!</p> <p>Python 2.4 compatibility, contributed by Charles Leifer. Thanks!</p>
</div> </div>
<div class="section" id="id4"> <div class="section" id="id5">
<h3>Bugfixes</h3> <h3>Bugfixes</h3>
<p>Fix: The exception &quot;...has no attribute 'sub_and_superclass_dict'&quot; <p>Fix: The exception &quot;...has no attribute 'sub_and_superclass_dict'&quot;
could be raised. (This occurred if a subclass defined __init__ could be raised. (This occurred if a subclass defined __init__
@ -429,7 +440,7 @@ Now it is possible to give a field the same name as the class
</div> </div>
</div> </div>
<hr class="docutils" /> <hr class="docutils" />
<div class="section" id="id5"> <div class="section" id="id6">
<h2>2010-2-4</h2> <h2>2010-2-4</h2>
<div class="section" id="new-features-and-documentation"> <div class="section" id="new-features-and-documentation">
<h3>New features (and documentation)</h3> <h3>New features (and documentation)</h3>
@ -442,7 +453,7 @@ only() allowed (but not yet supported)</p>
<p>More about these additions in the docs: <p>More about these additions in the docs:
<a class="reference external" href="http://bserve.webhop.org/wiki/django_polymorphic/doc">http://bserve.webhop.org/wiki/django_polymorphic/doc</a></p> <a class="reference external" href="http://bserve.webhop.org/wiki/django_polymorphic/doc">http://bserve.webhop.org/wiki/django_polymorphic/doc</a></p>
</div> </div>
<div class="section" id="id6"> <div class="section" id="id7">
<h3>Bugfixes</h3> <h3>Bugfixes</h3>
<ul class="simple"> <ul class="simple">
<li>fix remaining potential accessor name clashes (but this only works <li>fix remaining potential accessor name clashes (but this only works
@ -467,7 +478,7 @@ support for natural keys in serialization).</li>
</div> </div>
</div> </div>
<hr class="docutils" /> <hr class="docutils" />
<div class="section" id="id7"> <div class="section" id="id8">
<h2>2010-1-30</h2> <h2>2010-1-30</h2>
<p>Fixed ContentType related field accessor clash (an error emitted <p>Fixed ContentType related field accessor clash (an error emitted
by model validation) by adding related_name to the ContentType by model validation) by adding related_name to the ContentType
@ -475,7 +486,7 @@ ForeignKey. This happened if your polymorphc model used a ContentType
ForeignKey. Thanks to Andrew Ingram.</p> ForeignKey. Thanks to Andrew Ingram.</p>
</div> </div>
<hr class="docutils" /> <hr class="docutils" />
<div class="section" id="id8"> <div class="section" id="id9">
<h2>2010-1-29</h2> <h2>2010-1-29</h2>
<p>Restructured django_polymorphic into a regular Django add-on <p>Restructured django_polymorphic into a regular Django add-on
application. This is needed for the management commands, and application. This is needed for the management commands, and
@ -486,7 +497,7 @@ as well (and it makes sure the tests are always included).</p>
(&quot;installation/testing&quot;) for more info.</p> (&quot;installation/testing&quot;) for more info.</p>
</div> </div>
<hr class="docutils" /> <hr class="docutils" />
<div class="section" id="id9"> <div class="section" id="id10">
<h2>2010-1-28</h2> <h2>2010-1-28</h2>
<p>Added the polymorphic_dumpdata management command (github issue 4), <p>Added the polymorphic_dumpdata management command (github issue 4),
for creating fixtures, this should be used instead of for creating fixtures, this should be used instead of
@ -497,7 +508,7 @@ needs Django 1.2 (important as any polymorphic model uses
ContentType).</p> ContentType).</p>
</div> </div>
<hr class="docutils" /> <hr class="docutils" />
<div class="section" id="id10"> <div class="section" id="id11">
<h2>2010-1-26</h2> <h2>2010-1-26</h2>
<p>IMPORTANT - database schema change (more info in change log). <p>IMPORTANT - database schema change (more info in change log).
I hope I got this change in early enough before anyone started I hope I got this change in early enough before anyone started

View File

@ -3,6 +3,18 @@
Changelog Changelog
++++++++++ ++++++++++
2011-01-24 V1.0 Release Candidate 1
===================================
Bugfixes
------------------------
* Fixed GitHub issue 15 (query result incomplete with inheritance).
Thanks to John Debs for reporting and the test case.
------------------------------------------------------------------
2010-11-11 V1.0 Beta 2 2010-11-11 V1.0 Beta 2
====================== ======================

View File

@ -76,6 +76,13 @@ class PolymorphicModelBase(ModelBase):
# for __init__ function of this class (monkeypatching inheritance accessors) # for __init__ function of this class (monkeypatching inheritance accessors)
new_class.polymorphic_super_sub_accessors_replaced = False new_class.polymorphic_super_sub_accessors_replaced = False
# determine the name of the primary key field and store it into the class variable
# polymorphic_primary_key_name (it is needed by query.py)
for f in new_class._meta.fields:
if f.primary_key and type(f)!=models.OneToOneField:
new_class.polymorphic_primary_key_name=f.name
break
return new_class return new_class
def get_inherited_managers(self, attrs): def get_inherited_managers(self, attrs):

View File

@ -161,7 +161,9 @@ class PolymorphicQuerySet(QuerySet):
# We get different type(o.pk) in this case. # We get different type(o.pk) in this case.
# We work around this by using the real name of the field directly # We work around this by using the real name of the field directly
# for accessing the primary key of the the derived objects. # for accessing the primary key of the the derived objects.
pk_name = self.model._meta.pk.name # We might assume that self.model._meta.pk.name gives us the name of the primary key field,
# but it doesn't. Therefore we use polymorphic_primary_key_name, which we set up in base.py.
pk_name = self.model.polymorphic_primary_key_name
# For each model in "idlist_per_model" request its objects (the real model) # For each model in "idlist_per_model" request its objects (the real model)
# from the db and store them in results[]. # from the db and store them in results[].
@ -184,7 +186,7 @@ class PolymorphicQuerySet(QuerySet):
for select_field_name in self.query.extra_select.keys(): for select_field_name in self.query.extra_select.keys():
attr = getattr(base_result_objects_by_id[o_pk], select_field_name) attr = getattr(base_result_objects_by_id[o_pk], select_field_name)
setattr(o, select_field_name, attr) setattr(o, select_field_name, attr)
results[o_pk] = o results[o_pk] = o
# re-create correct order and return result list # re-create correct order and return result list

View File

@ -156,6 +156,14 @@ class InitTestModelSubclass(InitTestModel):
def x(self): def x(self):
return 'XYZ' return 'XYZ'
# models from github issue
class Top(PolymorphicModel):
name = models.CharField(max_length=50)
class Middle(Top):
description = models.TextField()
class Bottom(Middle):
author = models.CharField(max_length=50)
# UUID tests won't work with Django 1.1 # UUID tests won't work with Django 1.1
if not (django_VERSION[0] <= 1 and django_VERSION[1] <= 1): if not (django_VERSION[0] <= 1 and django_VERSION[1] <= 1):
@ -610,6 +618,20 @@ __test__ = {"doctest": """
#>>> print 'DiamondXY fields 1: field_b "%s", field_x "%s", field_y "%s"' % (o.field_b, o.field_x, o.field_y) #>>> print 'DiamondXY fields 1: field_b "%s", field_x "%s", field_y "%s"' % (o.field_b, o.field_x, o.field_y)
#DiamondXY fields 1: field_b "a", field_x "x", field_y "y" #DiamondXY fields 1: field_b "a", field_x "x", field_y "y"
# test for github issue
>>> t = Top()
>>> t.save()
>>> m = Middle()
>>> m.save()
>>> b = Bottom()
>>> b.save()
>>> Top.objects.all()
[<Top: Top object>, <Middle: Middle object>, <Bottom: Bottom object>]
>>> Middle.objects.all()
[<Middle: Middle object>, <Bottom: Bottom object>]
>>> Bottom.objects.all()
[<Bottom: Bottom object>]
>>> settings.DEBUG=False >>> settings.DEBUG=False