DjangoDjango

Russian Slugs for Django Taggit

Feb 11, 2014 · Updated: Jul 12, 2021 · 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, ru_taggit.py 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(self.name))[:128]


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

    @classmethod
    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).

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓