For some time, there was one re-usable Django tagging application — django-tagging — and that was it. If you didn’t like it, you rolled your own. It was certainly a decent application. You could pretty easily tag anything and provided some decent features out of the box. However, very recently, another app showed up: django-taggit. This post is going to compare and contrast the two and why I decided to switch my work project to django-taggit.
Old school tagging
Back in March when we were just starting out, I went with django-tagging because that was the only tagging app around. Back then I didn’t think about it, but now 6 months later there have been no new updates or releases to django-tagging. I think this really led to the creation of django-taggit. Django-tagging had some nice features that were pretty useful like a template tag for a tag cloud. While django-tagging got me up and running quickly, it wasn’t without its annoyances. Deleting an object left dangling references to its tags since those would not be deleted because it used generic foreign keys. There was an issue filed for this back in 2008 that was never resolved. It resulted in me having to override the delete method of every object that got tagged. If I had 3 objects that got tagged, I repeated the same snippet 3 times!
1 2 3 4 5 6 7 |
# Every tagged model with django-tagging needs this code to properly clean up def delete(self): # Deleting all asociated tags. Tag.objects.update_tags(self, None) super(MyModel, self).delete() |
The fact that such seemingly important functionality had not been added since 2008 pointed to the fact that django-tagging had been left fallow for a while.
New school tagging
At Djangocon, I first heard about django-taggit. Immediately, I liked the fact that the docs were a little bit more fleshed out than django-tagging’s docs. In addition, I found the search API for taggit to be much more intuitive.
1 2 3 4 5 6 7 8 9 10 11 |
## Django-tagging: requires a level of indirection through the TaggedItem model from tagging.models import Tag, TaggedItem from myapp.blog.models import Post hacking_tag = Tag.objects.get(name='hacking') TaggedItem.objects.get_by_model(Post, hacking_tag) ## Django-taggit: look directly through the model you are searching from myapp.blog.models import Post Post.objects.filter(tags__name__in=["hacking"]) |
The little things
The nicest part about django-taggit is that it integrates much better with the admin. To tag an object in the admin with django-tagging, I would need to figure out the primary key id of the object I want to tag and then go to the TaggedItem admin and then tag it by id. It was unintuitive and error prone. With django-taggit, it’s as easy as editing an object the normal way. A “tags” field shows up and it explains that it simply accepts a comma separated list.
The one feature I liked from django-tagging that taggit doesn’t implement is the tag cloud. I can understand that different folks want clouds done slightly differently and that it’s not a feature that has one right way to do it. However, it was pretty damn convenient.
All told, django-taggit seems to do the job that I want it to do and it stays out of the way otherwise. It’s much more intuitive to setup and use. It’s actively maintained and the docs are better. There’s nothing not to like. At the same time, I don’t want to say it’s the end all. I’d love to see the django-tagging guys come back with some great new features because apps can always be better.
Not used django-taggit myself, but thought I should point out that your assessment of how to tag an item with django-tagging is completely wrong.
It can work the same way as you describe the method for django-taggit.
By adding a model field similar to the following, will get you a field in the admin console with which you enter comma separated values.
tags_string = tagging_fields.TagField(max_length=255, verbose_name=_(“tag string”))
Going back and looking at the documentation I now see this. I’d always used the generic foreign keys with django-tagging. However, this harks back to quality of documentation and tutorials.
Did you migrate your django-tagging data into django-taggit? If so do you have a write-up for that?
I didn’t have much to migrate so I just used the Django interactive shell.