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)
Change History (17)
comment:1 by , 18 years ago
comment:2 by , 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 , 18 years ago
Triage Stage: | Unreviewed → Accepted |
---|
by , 17 years ago
Moved the order_with_respect_to curried methods so that they're added after model resolution
comment:5 by , 17 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
Status: | new → 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 by , 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)
by , 17 years ago
Attachment: | django-bug2740-patchfor-r7534.patch added |
---|
update of patch for current trunk
comment:8 by , 17 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:10 by , 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:12 by , 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 , 15 years ago
Triage Stage: | Accepted → Ready 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 , 14 years ago
Needs tests: | set |
---|---|
Triage Stage: | Ready for checkin → 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 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
It looks like this has been fixed by #13241.
I just ran into this problem myself. Any chance on it being fixed in the next release or two?