Search code examples
pythondjangodjango-taggitdjango-modeltranslation

How to add translation fields in django-taggit?


I am working on multi language web-site. In my apps I am using django modeltranslation to set values on different languages from admin. So how can I add modeltranslation into django-taggit to have a tag on different languages?


Solution

  • Well I came around, when I was looking for the same issue:

    #models.py
    @register_snippet
    class BlogTag(TagBase):
        class Meta:
            verbose_name = "tag"
            verbose_name_plural = "tags"
    
        #this is for editing in wagtail. For django not needed
        panels =  [
            FieldPanel('name'),
            FieldPanel('slug'),
        ]
    
    class BlogPageTag(ItemBase):
        tag = models.ForeignKey(
            BlogTag, related_name="tagged_blogs", on_delete=models.CASCADE
        )
        content_object = ParentalKey('BlogPage', related_name='post_tags')
    
    from modelcluster.tags import ClusterTaggableManager
    
    class BlogPage(Page):
        tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
    

    And in the translation.py:

    #translation.py
    from .models import BlogPage,BlogTag
    from django_modeltranslation.translation import TranslationOptions,register
    
    
    @register(BlogPage)
    class BlogPageTR(TranslationOptions):
        fields = (
           'intro','body',
        )
    
    @register(BlogTag)
    class BlogTagTR(TranslationOptions):
        fields = (
          'name',
        )