Specifying an order_with_respect_to for a self referential one to many relationship fails
|Reported by:||anonymous||Owned by:||Gulopine|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||no|
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))
Change History (17)
Changed 9 years ago by Gulopine
comment:5 Changed 9 years ago by Gulopine
- Has patch set
- Owner changed from nobody to Gulopine
- Status changed from new to assigned
Changed 8 years ago by kcarnold
comment:13 Changed 6 years ago by adamnelson
- Triage Stage changed from Accepted to Ready for checkin
comment:14 Changed 6 years ago by mtredinnick
- Needs tests set
- Triage Stage changed from Ready for checkin to Accepted