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: Marty Alchin
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:

Description

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:
      pass
      
   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)
      else:
         return self.name
         
      return '-->'.join((prefix,self.name))

Attachments (2)

2740.diff (6.0 KB) - added by Marty Alchin 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 Kenneth Arnold 9 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: 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 Chris Beaven

Triage Stage: UnreviewedAccepted

comment:4 Changed 9 years ago by anonymous

Related to #13

Changed 9 years ago by Marty Alchin

Attachment: 2740.diff added

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

comment:5 Changed 9 years ago by Marty Alchin

Has patch: set
Owner: changed from nobody to Marty Alchin
Status: newassigned

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 9 years ago by Jacob

Also see #6402 for another patch.

Changed 9 years ago by Kenneth Arnold

update of patch for current trunk

comment:8 Changed 9 years ago by Kenneth Arnold

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 Werner Beroux

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 http://groups.google.com/group/django-developers/browse_thread/thread/280bca5001faca07

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:

http://thefekete.net/blog/sorting-hierarchical-categories-in-django/

comment:13 Changed 7 years ago by Adam Nelson

Triage Stage: AcceptedReady for checkin

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

http://www.eflorenzano.com/blog/post/exploring-mixins-django-model-inheritance/

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

comment:14 Changed 6 years ago by Malcolm Tredinnick

Needs tests: set
Triage Stage: Ready for checkinAccepted

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 Kyle Dodson

Resolution: fixed
Status: assignedclosed

It looks like this has been fixed by #13241.

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