Skip to content

Commit ae448e4

Browse files
committed
[1.2.X] Fixed #13241. order_with_respect_to now works with ForeignKeys who refer to their model lazily (i.e. with a string). Thanks to Gabriel Grant for the patch. This is a backport of [14045].
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14046 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 3ab1a0c commit ae448e4

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ answer newbie questions, and generally made Django that much better:
197197
David Gouldin <[email protected]>
198198
199199
Collin Grady <[email protected]>
200+
Gabriel Grant <[email protected]>
200201
Simon Greenhill <[email protected]>
201202
Owen Griffiths
202203
Espen Grindhaug <http://grindhaug.org/>

django/db/models/base.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError, ValidationError, NON_FIELD_ERRORS
66
from django.core import validators
77
from django.db.models.fields import AutoField, FieldDoesNotExist
8-
from django.db.models.fields.related import OneToOneRel, ManyToOneRel, OneToOneField
8+
from django.db.models.fields.related import (OneToOneRel, ManyToOneRel,
9+
OneToOneField, add_lazy_relation)
910
from django.db.models.query import delete_objects, Q
1011
from django.db.models.query_utils import CollectedObjects, DeferredAttribute
1112
from django.db.models.options import Options
@@ -223,8 +224,25 @@ def _prepare(cls):
223224
if opts.order_with_respect_to:
224225
cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True)
225226
cls.get_previous_in_order = curry(cls._get_next_or_previous_in_order, is_next=False)
226-
setattr(opts.order_with_respect_to.rel.to, 'get_%s_order' % cls.__name__.lower(), curry(method_get_order, cls))
227-
setattr(opts.order_with_respect_to.rel.to, 'set_%s_order' % cls.__name__.lower(), curry(method_set_order, cls))
227+
# defer creating accessors on the foreign class until we are
228+
# certain it has been created
229+
def make_foreign_order_accessors(field, model, cls):
230+
setattr(
231+
field.rel.to,
232+
'get_%s_order' % cls.__name__.lower(),
233+
curry(method_get_order, cls)
234+
)
235+
setattr(
236+
field.rel.to,
237+
'set_%s_order' % cls.__name__.lower(),
238+
curry(method_set_order, cls)
239+
)
240+
add_lazy_relation(
241+
cls,
242+
opts.order_with_respect_to,
243+
opts.order_with_respect_to.rel.to,
244+
make_foreign_order_accessors
245+
)
228246

229247
# Give the class a docstring -- its definition.
230248
if cls.__doc__ is None:

tests/modeltests/order_with_respect_to/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ class Meta:
1717

1818
def __unicode__(self):
1919
return unicode(self.text)
20+
21+
class Post(models.Model):
22+
title = models.CharField(max_length=200)
23+
parent = models.ForeignKey("self", related_name="children", null=True)
24+
25+
class Meta:
26+
order_with_respect_to = "parent"
27+
28+
def __unicode__(self):
29+
return self.title

tests/modeltests/order_with_respect_to/tests.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.test import TestCase
44

5-
from models import Question, Answer
5+
from models import Post, Question, Answer
66

77

88
class OrderWithRespectToTests(TestCase):
@@ -60,3 +60,12 @@ def test_basic(self):
6060
],
6161
attrgetter("text")
6262
)
63+
64+
def test_recursive_ordering(self):
65+
p1 = Post.objects.create(title='1')
66+
p2 = Post.objects.create(title='2')
67+
p1_1 = Post.objects.create(title="1.1", parent=p1)
68+
p1_2 = Post.objects.create(title="1.2", parent=p1)
69+
p2_1 = Post.objects.create(title="2.1", parent=p2)
70+
p1_3 = Post.objects.create(title="1.3", parent=p1)
71+
self.assertEqual(p1.get_post_order(), [p1_1.pk, p1_2.pk, p1_3.pk])

0 commit comments

Comments
 (0)