Django-taggit with Django-treebeard or suitable Taxonomy app

533 views Asked by At

Environment: Python 2.7.10 Django 1.11.3

Problem I need a Taxonomy system instead of just tags. For example:

  • Art > Digital Illustration
  • Programming > Websites > Code Fights
  • Programming > Websites > Hackerrank
  • Programming > Websites > Code Fights > Arcade > Level One
  • Programming > Code Challenges

Looking around I found this post in StackOverflow which led me to this blog post.

Which led me to the following code:

Models

class Taxonomy (TagBase, MP_Node):
    node_order_by = ['name']

class TaggedPost (ItemBase):
    content_object = models.ForeignKey('Post')
    tag = models.ForeignKey('Taxonomy', related_name='tags')

class Post(ItemBase):
    tags = TaggableManager(through=TaggedPost, blank=True)

Admin

admin.site.register(Taxonomy, TreeAdmin)

Problem is when I go to admin to add the Taxonomies I get the following fields:

  • Name
  • Slug
  • Path
  • Depth
  • Numchild

The last two are not meant to be input by hand, so says the documentation of tree beard.

Do not change the values of path, depth or numchild directly: use one of the included methods instead. Consider these values read-only.

I went ahead and tried to input them myself, for the curiosity/stupidity, and the tree broke. So I had to use this hack to fix it:

f = Taxonomy.objects.all()
from django.db.models import QuerySet
QuerySet.delete(f)

Kudos to user “jrief” for the hack.

Another problem is the fact I can no longer access my Post model via admin. I get the following error:

FieldDoesNotExist at /admin/main/post Post has no attribute 'content_object'

Keep in mind this is my first Django project and app. Appreciate any ideas you might have or a good django app for taxonomies, or an idea as of how to implement the taxonomies I need.

1

There are 1 answers

0
Chayemor On BEST ANSWER

I ended up using the app Django Category and it's been brilliant. Just follow easy-as-pie instructions (barely any steps!) and everything works as expected.