Specifying an order_with_respect_to for a self referential one to many relationship fails
|Reported by:||anonymous||Owned by:||Marty Alchin|
|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)
comment:5 Changed 9 years ago by
|Owner:||changed from nobody to Marty Alchin|
|Status:||new → assigned|