Ticket #13241: 13241_unittest.diff
File 13241_unittest.diff, 3.5 KB (added by , 14 years ago) |
---|
-
django/db/models/base.py
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, OneToOneField, add_lazy_relation 9 9 from django.db.models.query import delete_objects, Q 10 10 from django.db.models.query_utils import CollectedObjects, DeferredAttribute 11 11 from django.db.models.options import Options … … 223 223 if opts.order_with_respect_to: 224 224 cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True) 225 225 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)) 226 # defer creating accessors on the foreign class until we are certain it has been created 227 def make_foreign_order_accessors(field, model, cls): 228 setattr(field.rel.to, 'get_%s_order' % cls.__name__.lower(), curry(method_get_order, cls)) 229 setattr(field.rel.to, 'set_%s_order' % cls.__name__.lower(), curry(method_set_order, cls)) 230 add_lazy_relation(cls, opts.order_with_respect_to, opts.order_with_respect_to.rel.to, make_foreign_order_accessors) 228 231 229 232 # Give the class a docstring -- its definition. 230 233 if cls.__doc__ is None: -
tests/modeltests/order_with_respect_to/tests.py
1 from django.test import TestCase 2 from modeltests.order_with_respect_to.models import Post 3 4 class RecursiveTests(TestCase): 5 def test_recursive_ordering(self): 6 p1 = Post(title='1') 7 p1.save() 8 p2 = Post(title='2') 9 p2.save() 10 p1_1 = Post(title="1.1", parent=p1) 11 p1_1.save() 12 p1_2 = Post(title="1.2", parent=p1) 13 p1_2.save() 14 p2_1 = Post(title="2.1", parent=p2) 15 p2_1.save() 16 p1_3 = Post(title="1.3", parent=p1) 17 p1_3.save() 18 self.assertEqual(p1.get_post_order(), [3,4,6]) -
tests/modeltests/order_with_respect_to/models.py
76 76 77 77 """ 78 78 } 79 80 81 class Post(models.Model): 82 title = models.CharField(max_length=200) 83 parent = models.ForeignKey('Post', related_name="children", null=True) 84 # ... more fields 85 86 class Meta: 87 order_with_respect_to = 'parent' 88 89 def __unicode__(self): 90 return unicode(self.title) 91 -
AUTHORS
513 513 Gasper Zejn <zejn@kiberpipa.org> 514 514 Jarek Zgoda <jarek.zgoda@gmail.com> 515 515 Cheng Zhang 516 Gabriel Grant <g@briel.ca> 516 517 517 518 A big THANK YOU goes to: 518 519