Russian Slugs for Django Taggit

Feb 11, 2014 · by Tim Kamanin

It appears that Django Taggit doesn't support transliteratable tags, what we need to do is to add such support, how you may ask? We should use proxy models. Just add this piece of code to, let's say, file:

from django.template.defaultfilters import slugify
from taggit.models import Tag, TaggedItem
from djavto.settings.base import SLUG_TRANSLITERATOR

class RuTag(Tag):
  class Meta:
    proxy = True

  def slugify(self, tag, i=None):
    return slugify(SLUG_TRANSLITERATOR([:128]

class RuTaggedItem(TaggedItem):
  class Meta:
    proxy = True

  def tag_model(cls):
    return RuTag

You may note 'SLUG_TRANSLITERATOR' setting, it can be any function that does a transliteration, in my case it is set to SLUG_TRANSLITERATOR = unicoded.unicode Then in your model, do the following:

class BlogPost(models.Model):
  #fields go here
  tags = TaggableManager(through=RuTaggedItem)

And that's all!

NOTE: You can use this approach with any other languages that need transliteration, not only with Russian (Cyrillic).

Want to get more 🔥 tips like this one?

Subscribe to get notified about new dev tutorials