Russian Slugs for Django Taggit

11 Feb · by Tim Kamanin · 1 min read

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

Comments

Required for comment verification



belek

What library needed for using "unicoded.unicode" ?

Reply · 2 years, 5 months ago