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

Fallback behavior of virtual fields inconsistent with behavior when used in expressions #84

Open
bbliem opened this issue May 12, 2022 · 0 comments

Comments

@bbliem
Copy link

bbliem commented May 12, 2022

Assume you have the settings

LANGUAGE_CODE = "en"
MODELTRANS_FALLBACK = {"default": ("de",)},

and the following model instance:

blog = Blog.objects.create(title="foo")

The following example shows that the fallback behavior of TranslatedVirtualField.__get__() and TranslatedVirtualField.as_expression() is different, which is in my opinion not intuitive:

>>> with override("de"): print(blog.title_i18n)
foo
>>> with override("de"): print(Blog.objects.filter(id=blog.id).values("title_i18n"))
<MultilingualQuerySet [{'title_i18n': None}]>

This is because __get__() falls back to the original field value, whereas as_expression() only considers the languages in the fallback chain but not the original field value.

Adding the following before the Coalesce in as_expression() would fix that:

lookups.append(bare_lookup.replace(self.name, self.original_name))

This would, in addition, also fix the error that you get in as_expression() when you specify an empty fallback chain by MODELTRANS_FALLBACK = {"default": ()}:

ValueError: Coalesce must take at least two expressions
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