deschler / django-modeltranslation

Translates Django models using a registration approach.
BSD 3-Clause "New" or "Revised" License
1.38k stars 257 forks source link

Fallback shouldn't apply all fields #533

Open ozanteoman opened 4 years ago

ozanteoman commented 4 years ago

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)
last-partizan commented 4 years ago

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.

ozanteoman commented 4 years ago

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.

last-partizan commented 4 years ago

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}