On Jul 2, 12:22 pm, alain31 <asoy...@free.fr> wrote:
> Hello, I have a category model with a self foreign key:
>
> class Category(models.Model):
>     title = models.CharField(max_length=100)
>     parent = models.ForeignKey('self', blank=True, null=True,
> related_name = 'childs')
>     def __unicode__(self):
>         if not self.parent:
>             return self.title
>         else:
>             return self.parent.__unicode__() + u'/' + self.title
>
> I want to order (in admin and in querysets) categories with respect to
> the result of my __unicode__
> function (ex: /cat1/cat4 < /cat2/cat3). I tried ordering option in
> CategoryAdmin, Meta class with ordering
> but nothing worked ... May I add a __cmp__ function somewhere ?

No, because ordering of querysets is done in the database.

Your best bet is to use something like MPTT, which stores ordering
(via 'left' and 'right' fields) on each record in the tree.

Oh, and by the way, the last line is much better written like this:
    return u'%s/%s' % (self.parent, self.title)
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.

Reply via email to