Opened 17 years ago

Closed 17 years ago

#6402 closed (duplicate)

order_with_respect_to = 'some_field' fails if some_field = ForeignKey('self', ...)

Reported by: anonymous Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

A trivial change will fix this (add the method with setattr() to self if required):

--- django/db/models/base.py      (revision 7018)
+++ django/db/models/base.py      (working copy)
@@ -194,8 +195,16 @@
         if opts.order_with_respect_to:
             cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True)
             cls.get_previous_in_order = curry(cls._get_next_or_previous_in_order, is_next=False)
-            setattr(opts.order_with_respect_to.rel.to, 'get_%s_order' % cls.__name__.lower(), curry(method_get_order, cls))
-            setattr(opts.order_with_respect_to.rel.to, 'set_%s_order' % cls.__name__.lower(), curry(method_set_order, cls))
+            if type(opts.order_with_respect_to.rel.to) in types.StringTypes \
+                    and opts.order_with_respect_to.rel.to == \
+                        cls._meta.object_name:
+                target = cls
+            else:
+                target = opts.order_with_respect_to.rel.to
+            setattr(target, 'get_%s_order' % cls.__name__.lower(),
+                    curry(method_get_order, cls))
+            setattr(target, 'set_%s_order' % cls.__name__.lower(),
+                    curry(method_set_order, cls))
 
         # Give the class a docstring -- its definition.
         if cls.__doc__ is None:

Change History (3)

comment:1 by anonymous, 17 years ago

Needs tests: set

comment:2 by anonymous, 17 years ago

Looks like a duplicate of #2740. Leaving open for the fix provided.

comment:3 by Jacob, 17 years ago

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top