0

I use django-modeltranslation to translate some model fields to Dutch (nl) and French (fr).

Now, all has been working fine except for one model:

@register(Association)
class AssociationTranslationOptions(TranslationOptions):
    fields = (
        'title',
        'name',
        'description',
    )

In the database, I can see the three fields with their translated equivalents: title, title_nl, title_fr, name, name_nl, name_fr, description, description_nl and description_fr. When I update these translated fields in django admin, or in the shell, they are saved correctly. The problem occurs when I fetch this data from the database. So if I fetch this data with Association.objects.all(), Association.objects.get(id=1), or just look at it in the django admin detail view, I see for each translated field the default data. When I inspect the SQL query that Django executed, I see this strange query:

SELECT "member_association"."id",
       "member_association"."title",
       "member_association"."title",
       "member_association"."title",
       "member_association"."slug",
       "member_association"."name",
       "member_association"."name",
       "member_association"."name",
       "member_association"."description",
       "member_association"."description",
       "member_association"."description",
       "member_association"."mollie_api_key_test",
       "member_association"."mollie_api_key_live",
       "member_association"."mollie_profile_id",
       "member_association"."bank_account_number",
       "member_association"."created",
       "member_association"."last_modified"
  FROM "member_association"
 WHERE "member_association"."id" = '2'

As you can see, there is three times title, three times name and three times description, without the language suffix.

So for updating this model, django-modeltranslation works great but for fetching translated data from the database, it does not work for only one of my created models:

# WORKS
Association.objects.update(name_nl='naam', name_fr='nom')

# ALSO WORKS
association_obj.name_nl = 'naam'
association_obj.name_fr = 'nom'
association_obj.save()

# DOES NOT WORK
association_obj = Association.objects.get(id=1)
print(association_obj.name_nl) # prints 'naam' -> correct
print(association_obj.name_fr) # prints 'nom' -> incorrect

Can someone please help me with this error. I am already looking at this for days. I thought I would have made a typo somewhere but there is no typo to be found. I also tried deleting these fields, adding them again, syncing the database again with ./manage.py makemigrations, ./manage.py migrate, ./manage.py update_translation_fields and ./manage.py sync_translation_fields but still no success.

I am using python version 3.5.2, Django version 2.1.7 and django-modeltranslation version 0.14.4

  • I'm having similar problem, Python 3.7.3, Django 2.2.6 and modeltranslation 0.13.0. The issue seemed to appear out of nowhere in the last few days. I'll try to pinpoint the cause. – Mikko Jul 17 '20 at 13:37
  • It seems the problem was related to a database query that was run too early in program initialization. In our case, we made some DB queries when creating a Django form class. I modified the queries to class __init__ method, and problem disappeared. It might be some kind of race condition, where DB queries are made before modeltranslation has finished bootstrapping everything. – Mikko Jul 18 '20 at 15:07
  • Thanks! I'll look into it because it could be we also do some DB queries when creating a Django form class. – Frederik Smolders Jul 22 '20 at 07:16

0 Answers0