Django parler:相同翻译用于不同语言时出错

huangapple go评论61阅读模式
英文:

Django parler: error when same translation for different languages

问题

I've just added django-parler to my project and I'm wondering is it my bad or it's really impossible to have the same translation more than once.

Case I'm trying to add a translation for the Polish language for the word "Sport," in Polish it would be "Sport," just the same as in English (which I have by default in the app). When trying to add this, I'm getting an error both when adding from the admin panel and when loading a fixture. I know that I might leave it blank and it won't really be that bad; however, I need to have a translation for each single word. I'm assuming there is a constraint in parler.

Error in Admin:

Polish:
Please correct the error below.
Interests Group Translation with this Title already exists.
Interests Group Translation with this Title already exists.
Title: [Sport]

django-parler settings:

PARLER_LANGUAGES = {
    None: (
        {'code': 'en',}, # English
        {'code': 'pl',}, # Polish
        {'code': 'uk',}, # Ukrainian
    ),
    'default': {
        'fallbacks': ['en'],
        'hide_untranslated': False,
    }
}

Model:

class InterestsGroup(TranslatableModel):
    '''
    Group for storing similar interests (that are related to the same topic)
    '''
    translations = TranslatedFields(
        title=models.CharField(_('Title'), unique=True, max_length=40, null=False, blank=False)
    )

    class Meta:
        verbose_name = _('Interests Group')
        verbose_name_plural = _('Interests Groups')
        ordering = ['id']
英文:

EDIT: my bad, i had:

translations = TranslatedFields(
    title=models.CharField(_('Title'), unique=False, max_length=40, null=False, blank=False)
)

I had unique=True, that was the reason, so now the question is how do I require unique title for each language (for example, so there is only one 'Sport' for english language but not in general including all the translations), can I add constraint directly in models.py?

---------------------------------------------------

I've just added django-parlel to my project and I'm wondering is it my bad or it's really impossible to have same translation more then once.

Case I'm trying to add translation for Polish language for word "Sport", in polish it would be "Sport", just same as in English (which I have by default in app). When trying to add this getting error both when adding from admin panel and when loading fixture. I know that i might leave it blank and it won't really be that bad however I need to have translation for each single word.
I'm assuming there is a constraint in parlel

Error in Admin:

Polish:
  Please correct the error below.
    Interests Group Translation with this Title already exists.
    Interests Group Translation with this Title already exists.
Title: [Sport]

django-parlel settings:

PARLER_LANGUAGES = {
    None: (
        {'code': 'en',}, # English
        {'code': 'pl',}, # Polish
        {'code': 'uk',}, # Ukrainian
    ),
    'default': {
        'fallbacks': ['en'],
        'hide_untranslated': False,
    }
}

Model:

class InterestsGroup(TranslatableModel):
    '''
    Group for storing similar interests (that are related to same topic)
    '''
    translations = TranslatedFields(
        title=models.CharField(_('Title'), unique=True, max_length=40, null=False, blank=False)
    )

    class Meta:
        verbose_name = _('Interests Group')
        verbose_name_plural = _('Interests Groups')
        ordering = ['id'] 

答案1

得分: 1

这解决了我的问题:

translations = TranslatedFields(
    title=models.CharField(_('标题'), unique=False, max_length=40, null=False, blank=False),
    meta={'unique_together': [('language_code', 'title')]}  # 每种语言/翻译的唯一标题
)
英文:

Adding this solved my problem:

translations = TranslatedFields(
    title=models.CharField(_('Title'), unique=False, max_length=40, null=False, blank=False),
    meta={'unique_together': [('language_code', 'title')]}  # unique title for each single language/translation
)

答案2

得分: 0

我会使用这个modeltranslation包

我也使用了django-parler,我认为使用modeltranslation比django-parler更好。不会出现在不同语言中编写相同文本的问题,而且它还具有类似django-parler的选项卡视图。

英文:

I would use this modeltranslation package

I also used django-parler and I think it's better to use modeltranslation instead of django-parler. There will not be a problem about writing same text in different languages and it is also have tabbed view like django-parler

huangapple
  • 本文由 发表于 2023年1月9日 08:42:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定