Ticket #13241: 13241.diff

File 13241.diff, 3.1 KB (added by Gabriel Grant, 14 years ago)

Patch (with test) fixing the problem

  • 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/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
     92__test__['RECURSIVE_TESTS'] = """       
     93
     94>>> p1 = Post(title='1')
     95>>> p1.save()
     96>>> p2 = Post(title='2')
     97>>> p2.save()
     98>>> p1_1 = Post(title="1.1", parent=p1)
     99>>> p1_1.save()
     100>>> p1_2 = Post(title="1.2", parent=p1)
     101>>> p1_2.save()
     102>>> p2_1 = Post(title="2.1", parent=p2)
     103>>> p2_1.save()
     104>>> p1_3 = Post(title="1.3", parent=p1)
     105>>> p1_3.save()
     106>>> p1.get_post_order()
     107[3,4,6]
     108
     109"""
  • 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