On Sat, 2009-03-07 at 16:37 -0800, NicoEchániz wrote: > Hi, > > I have a model with a recursive relation: > > class Category(models.Model): > name = models.CharField(max_length=100, blank=False, > db_index=True) > parent_category = models.ForeignKey('self', > related_name='child_categories', blank=True, null=True) > > And I want it's members to be ordered with respect to the parent > category, like so: > Pets > Pets -> Dogs > Pets -> Cats > Vehicles > Vehicles -> cars > etc. > > So that when a product is added, the category selection displays in a > logical order.
Then you're doomed. SQL simply cannot handle arbitrary levels of nested ordering in a situation like this. Each level of ordering requires a table join in the query, joining the child table to the parent table (a copy of the same table each time). So the query cannot know in advance how many levels of ordering to include, since it doesn't know the depth of the hirarchy. This is why data structures to store tree-like hierarchies in databases include extra fields about the position. Look at the "django-mptt" and "treebeard" for some things you can either use out of the box or some ideas you can utilise. > > > So I added to my model definintion the following: > class Meta: > order_with_respect_to = 'parent_category' > > > All seems well, but when I run: python manage.py syncdb > > I get an error: > [...] > File "/usr/lib/python2.5/site-packages/django/db/models/base.py", > line 178, in _prepare > setattr(opts.order_with_respect_to.rel.to, 'get_%s_order' % > cls.__name__.lower(), curry(method_get_order, cls)) > AttributeError: 'str' object has no attribute 'get_categoty_order' So somewhere you've got a type, since if you read the error message, it says you've misspelled "category" (it has less "t"s than in that error message). Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---