Opened 10 years ago

Closed 6 years ago

#2740 closed defect (fixed)

Specifying an order_with_respect_to for a self referential one to many relationship fails

Reported by: anonymous Owned by: Gulopine
Component: Core (Other) Version: master
Severity: normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:


Code example follows. Basically I'm trying to create a tree heirarchy wherein a single category can be related to multiple sub categories which can be related to sub categories, etc.

The problem occurs when I try to make the default ordering use the parent category so as to keep all the categories grouped together.

The error I get is "'str' object has no attribute 'get_category_order'". I think this is because when the ForeignKey is used, the string "self" is passed in to make it self referential. But apparently the Meta class is not expecting this behavior. Any suggestions?

class Category(models.Model):
   class Admin:
   class Meta:
      order_with_respect_to = 'parent'
   is_active         = models.BooleanField(default=False)
   name              = models.CharField(maxlength=100)
   parent            = models.ForeignKey("self", blank=True,null=True)
   search_kewords    = models.TextField(blank=True)
   def __str__(self):
      if self.parent:
         prefix = str(self.parent)
      return '-->'.join((prefix,

Attachments (2)

2740.diff (6.0 KB) - added by Gulopine 9 years ago.
Moved the order_with_respect_to curried methods so that they're added after model resolution
django-bug2740-patchfor-r7534.patch (4.2 KB) - added by kcarnold 8 years ago.
update of patch for current trunk

Download all attachments as: .zip

Change History (17)

comment:1 Changed 10 years ago by greg@…

I just ran into this problem myself. Any chance on it being fixed in the next release or two?

comment:2 Changed 10 years ago by treeform

  • Version set to SVN

me too - the custom sortable support sux in django. Any thing on this topic yet? How do we fix this?

comment:3 Changed 10 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 9 years ago by anonymous

Related to #13

Changed 9 years ago by Gulopine

Moved the order_with_respect_to curried methods so that they're added after model resolution

comment:5 Changed 9 years ago by Gulopine

  • Has patch set
  • Owner changed from nobody to Gulopine
  • Status changed from new to assigned

I ran into this bug yesterday and decided to take a stab at a fix for it. This moves the methods related to order_with_respect_to so that they don't get installed until *after* the model is resolved, allowing 'self' references to be used with order_with_respect_to.

Technically this is slightly backwards-incompatible, though, but only in one specific scenario: If there is code listening for the class_prepared signal, and that code in turn expects these methods to be present, this patch may cause problems, because they won't necessarily be there.

Also included is a quick test to keep it from cropping up again.

comment:6 Changed 9 years ago by anonymous

I'm not sure if your patch was intended as such, but it has no affect on the ordering of the items within the Admin (eg: within a select box)

comment:7 Changed 8 years ago by jacob

Also see #6402 for another patch.

Changed 8 years ago by kcarnold

update of patch for current trunk

comment:8 Changed 8 years ago by kcarnold

I hit this bug myself, and updated the (almost trivial) patch for current SVN. I didn't copy over the testcase, but the one from the original patch should be fine.

comment:9 Changed 8 years ago by mushy

any news for this bug ?

comment:10 Changed 8 years ago by Wernight

Still having this issue. It was working right before 1.0 and now even with 1.02 still not working.

Seems another guy also has the problem

comment:11 Changed 7 years ago by thefekete

Also having exact same problem with 1.1.1, anybody have a work around?

comment:12 Changed 7 years ago by thefekete

I figured out a work-around to auto-update a sort field on save. You can see the details here:

comment:13 Changed 6 years ago by adamnelson

  • Triage Stage changed from Accepted to Ready for checkin

The code is fine - not sure if it makes sense anymore or if something like this should be added to contrib:

Nonetheless, ready for a core-contributor to take to the next step.

comment:14 Changed 6 years ago by mtredinnick

  • Needs tests set
  • Triage Stage changed from Ready for checkin to Accepted

Just because there might not be any existing tests for order_with_respect_to doesn't mean this patch gets away without writing some. No patch goes in without tests, so we need that for this case. Test case should fail before the main body changes are applied and pass afterwards, of course.

comment:15 Changed 6 years ago by seniorquico

  • Resolution set to fixed
  • Status changed from assigned to closed

It looks like this has been fixed by #13241.

Note: See TracTickets for help on using tickets.
Back to Top