Categories & Subcategories With Django

Categories & Subcategories With Django

[crayon-5904b1c9dffea003877540-i/]

class Admin:list_display = ('name', '_parents_repr')

def __str__(self):p_list = self._recurse_for_parents(self)p_list.append(self.name)return self.get_separator().join(p_list)def get_absolute_url(self):if self.parent_id:return "/tag/%s/%s/" % (self.parent.slug, self.slug)else:return "/tag/%s/" % (self.slug)def _recurse_for_parents(self, cat_obj):p_list = []if cat_obj.parent_id:p = cat_obj.parentp_list.append(p.name)more = self._recurse_for_parents(p)p_list.extend(more)if cat_obj == self and p_list:p_list.reverse()return p_listdef get_separator(self):return ' :: 'def _parents_repr(self):p_list = self._recurse_for_parents(self)return self.get_separator().join(p_list)_parents_repr.short_description = "Tag parents"

 

def save(self):p_list = self._recurse_for_parents(self)if self.name in p_list:raise validators.ValidationError("You must not save a category in itself!")super(Category, self).save()

http://code.djangoproject.com/wiki/CookBookCategoryDataModelPostMagic

Posted by voidet

Categorised under Django
Bookmark the permalink or leave a trackback.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

or