django-modeltranslation
django-modeltranslation copied to clipboard
The wrong language is returned if the default value is set on the field
We have a model with a field which has a default value:
class MyModel(models.Model):
description = models.CharField(
max_length=255,
default='My default text',
)
We have EN and DE as languages. The values in the database are like this:
description: NULL description_en: 'My default text' description_de: 'Mein standard Text'
If the current active language is EN, model.description will wrongly output 'Mein standard Text'.
The issue is here: https://github.com/deschler/django-modeltranslation/blame/master/modeltranslation/fields.py#L344
undefined
is set to the fields default. In line 350, the english value is discarded, because it is equal to the defaults value, so the next value (German) is returned.
A workaround for this issue is to specify a custom undefined value in the translations.py
fields = (
'description',
)
# Temporary fix for
# https://github.com/deschler/django-modeltranslation/issues/508
fallback_undefined = {field: '' for field in fields}