django-modeltranslation icon indicating copy to clipboard operation
django-modeltranslation copied to clipboard

Fallback shouldn't apply all fields

Open ozanteoman opened this issue 5 years ago • 3 comments

Hi everybody , i think i have a big problem I keep fields like title , rating ,popularity ,... on Production Model. my rating and popularity fields are integer and they will be null when they are created. And side of model translation , i am using Fallback to avoid null for title but i don't need fallback for rating , popularity , if they are null , fallback show other_language value which defined in tuple, but i want to show null value

settings.py

LANGUAGE_CODE = 'tr'

gettext = lambda s: s
LANGUAGES = (
    ('tr', _('Turkish')),
    ('ru', _('Russian')),
    ('es', _('Spanish')),
    ('ar', _('Arabic')),
)

MODELTRANSLATION_LANGUAGES = ('tr', 'ru', 'es', 'ar')
MODELTRANSLATION_AUTO_POPULATE = True
MODELTRANSLATION_ENABLE_FALLBACKS = True

here is translation.py

from modeltranslation.translator import translator, TranslationOptions
from cosmeclub.products.models import Product


class ProductTranslationOptions(TranslationOptions):
    fields = ('title', 'rating', 'popularity', 'weekly_popularity', 'subcategory_order', 'last_reviewed_at')
    required_languages = ('tr', 'ru', 'es', 'ar')


translator.register(Product, ProductTranslationOptions)

ozanteoman avatar Jan 05 '20 11:01 ozanteoman

Hi, if you are only want to translate title, maybe you should only translate title?

class ProductTranslationOptions(TranslationOptions):
    fields = ('title', )
    required_languages = ('tr', 'ru', 'es', 'ar')

Like this. And if you want to keep stats for different languages, you should use different approch.

last-partizan avatar Jan 08 '20 07:01 last-partizan

Thank you for answering @last-partizan . Can you say that both title and rating is added in fields and only apply fallback a specific field like title for django-modeltranslation . Is it possible ?

I have used different approach , but if it possible ,I think that with working django-modeltranslation is better.

ozanteoman avatar Jan 08 '20 07:01 ozanteoman

https://django-modeltranslation.readthedocs.io/en/latest/registration.html#TranslationOptions.fallback_values

You can try to use something like:

fallback_undefined = None
fallback_undefined = {'title': 'no title', 'rating': None}

last-partizan avatar Jan 08 '20 07:01 last-partizan