Recursive Relation in django-taggit
I have created the following TagBase and each category can have subcategory... Will this work? How can I override its add function in the TaggableManager? class Category(TagBase): parent = models.ForeignKey('self', blank=True, null=True, related_name='child') description = models.TextField(blank=True, help_text="Optional") class Meta: verbose_name = _('Category') verbose_name_plural = _('Categories') -- 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.
Re: Meta, order_with_respect_to for recursive relation
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 -~--~~~~--~~--~--~---
Meta, order_with_respect_to for recursive relation
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. 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' This 'str' the error refers to is the 'self' in the foreign key relation in the model. This happens whenever you try to order_with_respect_to a model that has not yet been defined and you refer to it as a string. Which can be solved in most cases by moving that definition up in the models.py file and removing the quotes. But in this case the relation is recursive and the only way to accomplish it is to pass the string argument 'self' to my ForeignKey. Has anyone stumbled upon this matter before? I googled and found people complaining about this but no solutions... I hope someone can shed some light on the matter... Thanks, NicoEchaniz PS: the documentation[1] doesn't explain if there's a way to make to ordering work for recursive relations. http://docs.djangoproject.com/en/dev/ref/models/options/#order-with-respect-to --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: recursive relation
On Jul 16, 3:36 pm, "Lic. José M. Rodriguez Bacallao" <[EMAIL PROTECTED]> wrote: > How can I make a recursive one-to-many relation? > > -- > Lic. José M. Rodriguez Bacallao According to http://www.djangoproject.com/documentation/model-api/#many-to-one-relationships ``To create a recursive relationship - an object that has a many-to- one relationship with itself - use models.ForeignKey('self').'' So basically, just take the one-to-many relationship you wanted, and *reverse* the roles. For example, if you had a Person object and you wanted to keep track of the multiple Person objects who were its "employees", simply reverse the roles. Make each Person object keep track of its "boss" instead. (Hopefully that's what you were looking for; if I misunderstood the question and you were looking for more "advanced" info, I probably won't be much help since I'm a novice myself) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
recursive relation
How can I make a recursive one-to-many relation? -- Lic. José M. Rodriguez Bacallao Cupet - Todos somos muy ignorantes, lo que ocurre es que no todos ignoramos lo mismo. Recuerda: El arca de Noe fue construida por aficionados, el titanic por profesionales - --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---