Code

Ticket #13241: 13241_unittest.diff

File 13241_unittest.diff, 3.5 KB (added by gg, 4 years ago)

doctest moved to unittest at the request of Alex Gaynor

Line 
1Index: django/db/models/base.py
2===================================================================
3--- django/db/models/base.py    (revision 13621)
4+++ django/db/models/base.py    (working copy)
5@@ -5,7 +5,7 @@
6 from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError, ValidationError, NON_FIELD_ERRORS
7 from django.core import validators
8 from django.db.models.fields import AutoField, FieldDoesNotExist
9-from django.db.models.fields.related import OneToOneRel, ManyToOneRel, OneToOneField
10+from django.db.models.fields.related import OneToOneRel, ManyToOneRel, OneToOneField, add_lazy_relation
11 from django.db.models.query import delete_objects, Q
12 from django.db.models.query_utils import CollectedObjects, DeferredAttribute
13 from django.db.models.options import Options
14@@ -223,8 +223,11 @@
15         if opts.order_with_respect_to:
16             cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True)
17             cls.get_previous_in_order = curry(cls._get_next_or_previous_in_order, is_next=False)
18-            setattr(opts.order_with_respect_to.rel.to, 'get_%s_order' % cls.__name__.lower(), curry(method_get_order, cls))
19-            setattr(opts.order_with_respect_to.rel.to, 'set_%s_order' % cls.__name__.lower(), curry(method_set_order, cls))
20+            # defer creating accessors on the foreign class until we are certain it has been created
21+            def make_foreign_order_accessors(field, model, cls):
22+                setattr(field.rel.to, 'get_%s_order' % cls.__name__.lower(), curry(method_get_order, cls))
23+                setattr(field.rel.to, 'set_%s_order' % cls.__name__.lower(), curry(method_set_order, cls))
24+            add_lazy_relation(cls, opts.order_with_respect_to, opts.order_with_respect_to.rel.to, make_foreign_order_accessors)
25 
26         # Give the class a docstring -- its definition.
27         if cls.__doc__ is None:
28Index: tests/modeltests/order_with_respect_to/tests.py
29===================================================================
30--- tests/modeltests/order_with_respect_to/tests.py     (revision 0)
31+++ tests/modeltests/order_with_respect_to/tests.py     (revision 0)
32@@ -0,0 +1,18 @@
33+from django.test import TestCase
34+from modeltests.order_with_respect_to.models import Post
35+
36+class RecursiveTests(TestCase):
37+       def test_recursive_ordering(self):
38+               p1 = Post(title='1')
39+               p1.save()
40+               p2 = Post(title='2')
41+               p2.save()
42+               p1_1 = Post(title="1.1", parent=p1)
43+               p1_1.save()
44+               p1_2 = Post(title="1.2", parent=p1)
45+               p1_2.save()
46+               p2_1 = Post(title="2.1", parent=p2)
47+               p2_1.save()
48+               p1_3 = Post(title="1.3", parent=p1)
49+               p1_3.save()
50+               self.assertEqual(p1.get_post_order(), [3,4,6])
51Index: tests/modeltests/order_with_respect_to/models.py
52===================================================================
53--- tests/modeltests/order_with_respect_to/models.py    (revision 13621)
54+++ tests/modeltests/order_with_respect_to/models.py    (working copy)
55@@ -76,3 +76,16 @@
56 
57 """
58 }
59+
60+
61+class Post(models.Model):
62+    title = models.CharField(max_length=200)
63+    parent = models.ForeignKey('Post', related_name="children", null=True)
64+    # ... more fields
65+
66+    class Meta:
67+        order_with_respect_to = 'parent'
68+
69+    def __unicode__(self):
70+        return unicode(self.title)
71+
72Index: AUTHORS
73===================================================================
74--- AUTHORS     (revision 13621)
75+++ AUTHORS     (working copy)
76@@ -513,6 +513,7 @@
77     Gasper Zejn <zejn@kiberpipa.org>
78     Jarek Zgoda <jarek.zgoda@gmail.com>
79     Cheng Zhang
80+    Gabriel Grant <g@briel.ca>
81 
82 A big THANK YOU goes to:
83