【问题标题】:How to add translation fields in django-taggit?如何在 django-taggit 中添加翻译字段?
【发布时间】:2015-09-05 06:51:06
【问题描述】:

我正在开发多语言网站。在我的应用程序中,我使用 django modeltranslation 从管理员设置不同语言的值。那么如何将模型翻译添加到django-taggit 以在不同语言上添加标签?

【问题讨论】:

    标签: python django django-taggit django-modeltranslation


    【解决方案1】:

    好吧,当我在寻找同样的问题时,我来了:

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

    在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',
        )
    

    【讨论】:

      【解决方案2】:

      我认为您必须继承 TagBase 并添加自己的可翻译字段。

      【讨论】:

        猜你喜欢
        • 2020-11-05
        • 1970-01-01
        • 2018-11-13
        • 1970-01-01
        • 2018-01-30
        • 1970-01-01
        • 1970-01-01
        • 2015-06-03
        • 1970-01-01
        相关资源
        最近更新 更多