Skip to content

Commit 2c47db8

Browse files
committed
Fixed github issue 15 (query result incomplete with inheritance). Thanks to John Debs.
1 parent 3cc7d48 commit 2c47db8

File tree

5 files changed

+67
-13
lines changed

5 files changed

+67
-13
lines changed

CHANGES.html

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,17 @@ <h1><em>django_polymorphic</em></h1>
225225
</div>
226226
<div class="section" id="changelog">
227227
<h1>Changelog</h1>
228+
<div class="section" id="v1-0-release-candidate-1">
229+
<h2>2011-01-24 V1.0 Release Candidate 1</h2>
230+
<div class="section" id="bugfixes">
231+
<h3>Bugfixes</h3>
232+
<ul class="simple">
233+
<li>Fixed GitHub issue 15 (query result incomplete with inheritance).
234+
Thanks to John Debs for reporting and the test case.</li>
235+
</ul>
236+
</div>
237+
</div>
238+
<hr class="docutils" />
228239
<div class="section" id="v1-0-beta-2">
229240
<h2>2010-11-11 V1.0 Beta 2</h2>
230241
<div class="section" id="this-is-a-v1-0-testing-release">
@@ -283,7 +294,7 @@ <h3>New Features and API changes in Beta 2 since Beta 1</h3>
283294
</li>
284295
</ul>
285296
</div>
286-
<div class="section" id="bugfixes">
297+
<div class="section" id="id1">
287298
<h3>Bugfixes</h3>
288299
<ul class="simple">
289300
<li>Custom fields could cause problems when used as the primary key.
@@ -349,7 +360,7 @@ <h3>New Features</h3>
349360
</li>
350361
</ul>
351362
</div>
352-
<div class="section" id="id1">
363+
<div class="section" id="id2">
353364
<h3>Bugfixes</h3>
354365
<ul class="simple">
355366
<li>Removed requirement for primary key to be an IntegerField.
@@ -387,7 +398,7 @@ <h3>API Changes</h3>
387398
</div>
388399
</div>
389400
<hr class="docutils" />
390-
<div class="section" id="id2">
401+
<div class="section" id="id3">
391402
<h2>2010-2-22</h2>
392403
<p>IMPORTANT: API Changed (import path changed), and Installation Note</p>
393404
<p>The django_polymorphic source code has been restructured
@@ -411,11 +422,11 @@ <h2>2010-2-22</h2>
411422
<ul class="simple">
412423
<li>minor API addition: 'from polymorphic import VERSION, get_version'</li>
413424
</ul>
414-
<div class="section" id="id3">
425+
<div class="section" id="id4">
415426
<h3>New Features</h3>
416427
<p>Python 2.4 compatibility, contributed by Charles Leifer. Thanks!</p>
417428
</div>
418-
<div class="section" id="id4">
429+
<div class="section" id="id5">
419430
<h3>Bugfixes</h3>
420431
<p>Fix: The exception &quot;...has no attribute 'sub_and_superclass_dict'&quot;
421432
could be raised. (This occurred if a subclass defined __init__
@@ -429,7 +440,7 @@ <h3>Bugfixes</h3>
429440
</div>
430441
</div>
431442
<hr class="docutils" />
432-
<div class="section" id="id5">
443+
<div class="section" id="id6">
433444
<h2>2010-2-4</h2>
434445
<div class="section" id="new-features-and-documentation">
435446
<h3>New features (and documentation)</h3>
@@ -442,7 +453,7 @@ <h3>New features (and documentation)</h3>
442453
<p>More about these additions in the docs:
443454
<a class="reference external" href="http://bserve.webhop.org/wiki/django_polymorphic/doc">http://bserve.webhop.org/wiki/django_polymorphic/doc</a></p>
444455
</div>
445-
<div class="section" id="id6">
456+
<div class="section" id="id7">
446457
<h3>Bugfixes</h3>
447458
<ul class="simple">
448459
<li>fix remaining potential accessor name clashes (but this only works
@@ -467,15 +478,15 @@ <h3>&quot;Restrictions &amp; Caveats&quot; updated</h3>
467478
</div>
468479
</div>
469480
<hr class="docutils" />
470-
<div class="section" id="id7">
481+
<div class="section" id="id8">
471482
<h2>2010-1-30</h2>
472483
<p>Fixed ContentType related field accessor clash (an error emitted
473484
by model validation) by adding related_name to the ContentType
474485
ForeignKey. This happened if your polymorphc model used a ContentType
475486
ForeignKey. Thanks to Andrew Ingram.</p>
476487
</div>
477488
<hr class="docutils" />
478-
<div class="section" id="id8">
489+
<div class="section" id="id9">
479490
<h2>2010-1-29</h2>
480491
<p>Restructured django_polymorphic into a regular Django add-on
481492
application. This is needed for the management commands, and
@@ -486,7 +497,7 @@ <h2>2010-1-29</h2>
486497
(&quot;installation/testing&quot;) for more info.</p>
487498
</div>
488499
<hr class="docutils" />
489-
<div class="section" id="id9">
500+
<div class="section" id="id10">
490501
<h2>2010-1-28</h2>
491502
<p>Added the polymorphic_dumpdata management command (github issue 4),
492503
for creating fixtures, this should be used instead of
@@ -497,7 +508,7 @@ <h2>2010-1-28</h2>
497508
ContentType).</p>
498509
</div>
499510
<hr class="docutils" />
500-
<div class="section" id="id10">
511+
<div class="section" id="id11">
501512
<h2>2010-1-26</h2>
502513
<p>IMPORTANT - database schema change (more info in change log).
503514
I hope I got this change in early enough before anyone started

CHANGES.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
Changelog
44
++++++++++
55

6+
2011-01-24 V1.0 Release Candidate 1
7+
===================================
8+
9+
Bugfixes
10+
------------------------
11+
12+
* Fixed GitHub issue 15 (query result incomplete with inheritance).
13+
Thanks to John Debs for reporting and the test case.
14+
15+
16+
------------------------------------------------------------------
17+
618
2010-11-11 V1.0 Beta 2
719
======================
820

polymorphic/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ def __new__(self, model_name, bases, attrs):
7676
# for __init__ function of this class (monkeypatching inheritance accessors)
7777
new_class.polymorphic_super_sub_accessors_replaced = False
7878

79+
# determine the name of the primary key field and store it into the class variable
80+
# polymorphic_primary_key_name (it is needed by query.py)
81+
for f in new_class._meta.fields:
82+
if f.primary_key and type(f)!=models.OneToOneField:
83+
new_class.polymorphic_primary_key_name=f.name
84+
break
85+
7986
return new_class
8087

8188
def get_inherited_managers(self, attrs):

polymorphic/query.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ class self.model, but as a class derived from self.model. We want to re-fetch
161161
# We get different type(o.pk) in this case.
162162
# We work around this by using the real name of the field directly
163163
# for accessing the primary key of the the derived objects.
164-
pk_name = self.model._meta.pk.name
164+
# We might assume that self.model._meta.pk.name gives us the name of the primary key field,
165+
# but it doesn't. Therefore we use polymorphic_primary_key_name, which we set up in base.py.
166+
pk_name = self.model.polymorphic_primary_key_name
165167

166168
# For each model in "idlist_per_model" request its objects (the real model)
167169
# from the db and store them in results[].
@@ -184,7 +186,7 @@ class self.model, but as a class derived from self.model. We want to re-fetch
184186
for select_field_name in self.query.extra_select.keys():
185187
attr = getattr(base_result_objects_by_id[o_pk], select_field_name)
186188
setattr(o, select_field_name, attr)
187-
189+
188190
results[o_pk] = o
189191

190192
# re-create correct order and return result list

polymorphic/tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ class InitTestModelSubclass(InitTestModel):
156156
def x(self):
157157
return 'XYZ'
158158

159+
# models from github issue
160+
class Top(PolymorphicModel):
161+
name = models.CharField(max_length=50)
162+
class Middle(Top):
163+
description = models.TextField()
164+
class Bottom(Middle):
165+
author = models.CharField(max_length=50)
166+
159167

160168
# UUID tests won't work with Django 1.1
161169
if not (django_VERSION[0] <= 1 and django_VERSION[1] <= 1):
@@ -610,6 +618,20 @@ def show_base_manager(model):
610618
#>>> print 'DiamondXY fields 1: field_b "%s", field_x "%s", field_y "%s"' % (o.field_b, o.field_x, o.field_y)
611619
#DiamondXY fields 1: field_b "a", field_x "x", field_y "y"
612620
621+
# test for github issue
622+
>>> t = Top()
623+
>>> t.save()
624+
>>> m = Middle()
625+
>>> m.save()
626+
>>> b = Bottom()
627+
>>> b.save()
628+
>>> Top.objects.all()
629+
[<Top: Top object>, <Middle: Middle object>, <Bottom: Bottom object>]
630+
>>> Middle.objects.all()
631+
[<Middle: Middle object>, <Bottom: Bottom object>]
632+
>>> Bottom.objects.all()
633+
[<Bottom: Bottom object>]
634+
613635
614636
>>> settings.DEBUG=False
615637

0 commit comments

Comments
 (0)