Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store translated model to/from more than one database fails #331

Open
baudhuina opened this issue Jan 26, 2023 · 0 comments
Open

Store translated model to/from more than one database fails #331

baudhuina opened this issue Jan 26, 2023 · 0 comments

Comments

@baudhuina
Copy link

Hello,

My model is:

class InstrumentFamily(TranslatableModel):
    primary_key = True
    translations = TranslatedFields(
        label=CharNullField(_('Label'), max_length=100, unique=False, null=True,)

I have 2 database aliases 'default' and 'test' and my database router directs my model to 'test'.
I insert models in both databases by doing this:

fam = InstrumentFamily(code=TEST_CODE)
with switch_language(fam, 'en'):
    fam.label = "test_family_test EN"
with switch_language(fam, 'fr'):
    fam.label = "test_family_test FR"
fam.save()

which stores the object and its translations in database 'test', or by doing this:

fam = InstrumentFamily(code="TEST_FAM")
with switch_language(fam, 'en'):
    fam.label = "test_family_default_EN"
with switch_language(fam, 'fr'):
    fam.label = "test_family_default_FR"
fam.save(using='default')

which saves the object and its translations to database 'default'. So far, so good.

But when accessing the object previously saved in 'default' by doing this (after properly clearing all caches to force a database read):

fam = InstrumentFamily.objects.using('default').get(code=TEST_CODE)
print(f"  label: {fam.label}")

django-parler properly retrieves the object from database 'default', but looks for the translation from database 'test' ! (SQL trace below, see the very end of each line):

SELECT "orchestra_instrumentfamily"."id", "orchestra_instrumentfamily"."code" FROM "orchestra_instrumentfamily" WHERE "orchestra_instrumentfamily"."code" = 'TEST_FAM' LIMIT 21; args=('TEST_FAM',); alias=default
SELECT "orchestra_instrumentfamily_translation"."id", "orchestra_instrumentfamily_translation"."language_code", "orchestra_instrumentfamily_translation"."label", "orchestra_instrumentfamily_translation"."master_id" FROM "orchestra_instrumentfamily_translation" WHERE ("orchestra_instrumentfamily_translation"."master_id" = 34 AND "orchestra_instrumentfamily_translation"."language_code" = 'en') LIMIT 21; args=(34, 'en'); alias=test

This problem was originally reported on StackOverflow ([https://stackoverflow.com/questions/73792025/django-parler-how-to-store-and-retrieve-a-translated-model-to-from-more-than-on/74730082#74730082]) where the original author of django-parler (vdboor) confirmed this is likely to be a bug : django-parler doesn't pass the "using()" information to its internal queries that retrieve translation model data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant