diff --git a/AUTHORS b/AUTHORS
index bf63b10..7741fa6 100644
a
|
b
|
answer newbie questions, and generally made Django that much better:
|
197 | 197 | David Gouldin <dgouldin@gmail.com> |
198 | 198 | pradeep.gowda@gmail.com |
199 | 199 | Collin Grady <collin@collingrady.com> |
| 200 | Gabriel Grant <g@briel.ca> |
200 | 201 | Simon Greenhill <dev@simon.net.nz> |
201 | 202 | Owen Griffiths |
202 | 203 | Espen Grindhaug <http://grindhaug.org/> |
diff --git a/django/db/models/base.py b/django/db/models/base.py
index b3deda1..1fc3609 100644
a
|
b
|
import django.db.models.manager # Imported to register signal handler.
|
5 | 5 | from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError, ValidationError, NON_FIELD_ERRORS |
6 | 6 | from django.core import validators |
7 | 7 | 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) |
9 | 10 | from django.db.models.query import delete_objects, Q |
10 | 11 | from django.db.models.query_utils import CollectedObjects, DeferredAttribute |
11 | 12 | from django.db.models.options import Options |
… |
… |
class ModelBase(type):
|
223 | 224 | if opts.order_with_respect_to: |
224 | 225 | cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True) |
225 | 226 | 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 | ) |
228 | 246 | |
229 | 247 | # Give the class a docstring -- its definition. |
230 | 248 | if cls.__doc__ is None: |
diff --git a/tests/modeltests/order_with_respect_to/models.py b/tests/modeltests/order_with_respect_to/models.py
index 99c9f13..64a0a7c 100644
a
|
b
|
class Answer(models.Model):
|
17 | 17 | def __unicode__(self): |
18 | 18 | return unicode(self.text) |
19 | 19 | |
| 20 | class Post(models.Model): |
| 21 | title = models.CharField(max_length=200) |
| 22 | parent = models.ForeignKey("self", related_name="children", null=True) |
| 23 | |
| 24 | class Meta: |
| 25 | order_with_respect_to = "parent" |
| 26 | |
| 27 | def __unicode__(self): |
| 28 | return self.title |
| 29 | |
20 | 30 | __test__ = {'API_TESTS': """ |
21 | 31 | >>> q1 = Question(text="Which Beatle starts with the letter 'R'?") |
22 | 32 | >>> q1.save() |
diff --git a/tests/modeltests/order_with_respect_to/tests.py b/tests/modeltests/order_with_respect_to/tests.py
new file mode 100644
index 0000000..069aab1
-
|
+
|
|
| 1 | from django.test import TestCase |
| 2 | |
| 3 | from models import Post |
| 4 | |
| 5 | |
| 6 | class RecursiveTests(TestCase): |
| 7 | def test_recursive_ordering(self): |
| 8 | p1 = Post.objects.create(title='1') |
| 9 | p2 = Post.objects.create(title='2') |
| 10 | p1_1 = Post.objects.create(title="1.1", parent=p1) |
| 11 | p1_2 = Post.objects.create(title="1.2", parent=p1) |
| 12 | p2_1 = Post.objects.create(title="2.1", parent=p2) |
| 13 | p1_3 = Post.objects.create(title="1.3", parent=p1) |
| 14 | self.assertEqual(p1.get_post_order(), [3, 4, 6]) |