Ticket #13241: 13241_unittest.diff

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

doctest moved to unittest at the request of Alex Gaynor

  • django/db/models/base.py

     
    55from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError, ValidationError, NON_FIELD_ERRORS
    66from django.core import validators
    77from django.db.models.fields import AutoField, FieldDoesNotExist
    8 from django.db.models.fields.related import OneToOneRel, ManyToOneRel, OneToOneField
     8from django.db.models.fields.related import OneToOneRel, ManyToOneRel, OneToOneField, add_lazy_relation
    99from django.db.models.query import delete_objects, Q
    1010from django.db.models.query_utils import CollectedObjects, DeferredAttribute
    1111from django.db.models.options import Options
     
    223223        if opts.order_with_respect_to:
    224224            cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True)
    225225            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)
    228231
    229232        # Give the class a docstring -- its definition.
    230233        if cls.__doc__ is None:
  • tests/modeltests/order_with_respect_to/tests.py

     
     1from django.test import TestCase
     2from modeltests.order_with_respect_to.models import Post
     3
     4class 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

     
    7676
    7777"""
    7878}
     79
     80
     81class 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

     
    513513    Gasper Zejn <zejn@kiberpipa.org>
    514514    Jarek Zgoda <jarek.zgoda@gmail.com>
    515515    Cheng Zhang
     516    Gabriel Grant <g@briel.ca>
    516517
    517518A big THANK YOU goes to:
    518519
Back to Top