Opened 18 years ago

Closed 14 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: dev
Severity: normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

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 17 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 16 years ago.
update of patch for current trunk

Download all attachments as: .zip

Change History (17)

comment:1 by greg@…, 18 years ago

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

comment:2 by treeform, 18 years ago

Version: SVN

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

comment:3 by Chris Beaven, 18 years ago

Triage Stage: UnreviewedAccepted

comment:4 by anonymous, 17 years ago

Related to #13

by Marty Alchin, 17 years ago

Attachment: 2740.diff added

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

comment:5 by Marty Alchin, 17 years ago

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 by anonymous, 17 years ago

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

Also see #6402 for another patch.

by Kenneth Arnold, 16 years ago

update of patch for current trunk

comment:8 by Kenneth Arnold, 16 years ago

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 by mushy, 16 years ago

any news for this bug ?

comment:10 by Werner Beroux, 16 years ago

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 by thefekete, 15 years ago

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

comment:12 by thefekete, 15 years ago

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 by Adam Nelson, 15 years ago

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 by Malcolm Tredinnick, 14 years ago

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 by Kyle Dodson, 14 years ago

Resolution: fixed
Status: assignedclosed

It looks like this has been fixed by #13241.

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