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

Django dumpdata don't save initial translated field.

Open ghost opened this issue 8 years ago • 1 comments

I encounter a problem when I use the django command dumpdata.
The initial field on database aren't saved but remplace by the traducted field.

A short exemple:

models.py
---------
class Color(models.Model):

    id = models.AutoField(primary_key=True)
    term_name = models.CharField(max_length=90, verbose_name=_('Color|term_name'))

    def __str__(self):
        return self.term_name

    class Meta:
        db_table = 'color'
        verbose_name = _('Color')
        verbose_name_plural = _('Colors')
translation.py
--------------
class ColorOptions(TranslationOptions):
    fields = ('term_name',)
    empty_values = ''
translator.register(Color, ColorOptions)

Language on setting file:

settings.py
-----------
LANGUAGES = (
    ('en', 'English'),
    ('fr', 'Français'),
    ('nl', 'Nederlands'),
)

I put some data on database:

id term_name term_name_en term_name_fr term_name_nl
1 white
2 red red rouge rood
3 green green
4 blue bleu blauw
5 yellow purple gris grijs

And finaly the dumpdata python manage.py dumpdata --indent 2 --natural-foreign --output dump_file.json foo.color

The result file:

[
{
  "model": "foo.color",
  "pk": 1,
  "fields": {
    "term_name": "",
    "term_name_en": null,
    "term_name_fr": null,
    "term_name_nl": null
  }
},
{
  "model": "foo.color",
  "pk": 2,
  "fields": {
    "term_name": "red",
    "term_name_en": "red",
    "term_name_fr": "rouge",
    "term_name_nl": "rood"
  }
},
{
  "model": "foo.color",
  "pk": 3,
  "fields": {
    "term_name": "green",
    "term_name_en": "green",
    "term_name_fr": null,
    "term_name_nl": null
  }
},
{
  "model": "foo.color",
  "pk": 4,
  "fields": {
    "term_name": "",
    "term_name_en": null,
    "term_name_fr": "bleu",
    "term_name_nl": "blauw"
  }
},
{
  "model": "foo.color",
  "pk": 5,
  "fields": {
    "term_name": "purple",
    "term_name_en": "purple",
    "term_name_fr": "gris",
    "term_name_nl": "grijs"
  }
}
]

I'm using:
django==1.9.4 django-modeltranslation==0.12

ghost avatar Nov 10 '16 10:11 ghost

More info:

From my setting.py I have configured LANGUAGE_CODE = 'en'

Actually if I use administration web menu to add some items, they are an automatic transaction who copy source value to translated value with the LANGUAGE_CODE selected.

If I use shell mode to create and save value, this time, they are an automatic transaction who copy source value to the first language on the LANGUAGES parameter from settings.py.
And the same in the other way, translated field (first from LANGUAGES) to the source if it's not defined.

Sometime I use directly SQL to update database, that’s why I have some time no translated field in the previous example, so I read on the doc and find the command update_translation_fields how add translated field on DB but they are a problem with this command, it used the first LANGUAGES parameter from settings.py and not the LANGUAGE_CODE parameters.

So all this to say that all cmd from shell use the first language on the LANGUAGES parameter from settings.py and web application will use the LANGUAGE_CODE selected on settings.py.

Finally they are some trouble with this mix in the case of first LANGUAGES are not the same that LANGUAGE_CODE.
Adding data from web with only the source value without translate will create a translate but this one will be deleted if we use dumpdata cmd or will save data on other translated field if we use update_translation_fields cmd

ghost avatar Feb 09 '17 12:02 ghost