You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lookups are tranformed when the queryset definition is executed, which might be unexpected behavior with querysets.
An problematic use case might be defining the choices for a ModelChoiceField.
fromdjango.utils.translationimportoverridewithoverride('en'):
qs=Blog.objects.filter(title_i18n='foo')
print(str(qs.query))
# SELECT "app_blog"."id", "app_blog"."title", "app_blog"."body", "app_blog"."category_id", "app_blog"."i18n" # FROM "app_blog" # WHERE "app_blog"."title" = 'foo'withoverride('nl'):
print(str(qs.query))
# same querywithoverride('nl'):
qs=Blog.objects.filter(title_i18n='foo')
print(str(qs.query))
# SELECT "app_blog"."id", "app_blog"."title", "app_blog"."body", "app_blog"."category_id", "app_blog"."i18n", COALESCE(("app_blog"."i18n" ->> 'title_nl'), "app_blog"."title") AS "title_i18n_annotation" # FROM "app_blog" # WHERE COALESCE(("app_blog"."i18n" ->> 'title_nl'), "app_blog"."title") = 'foo'
Using a callable helps, but I'm not sure if the queryset argument of ModelChoiceField supports that:
qs=lambda: Blog.objects.filter(title_i18n='foo')
withoverride('nl'):
print(str(qs().query))
# SELECT "app_blog"."id", "app_blog"."title", "app_blog"."body", "app_blog"."category_id", "app_blog"."i18n", COALESCE(("app_blog"."i18n" ->> 'title_nl'), "app_blog"."title") AS "title_i18n_annotation" # FROM "app_blog" # WHERE COALESCE(("app_blog"."i18n" ->> 'title_nl'), "app_blog"."title") = 'foo'withoverride('en'):
print(str(qs().query))
# SELECT "app_blog"."id", "app_blog"."title", "app_blog"."body", "app_blog"."category_id", "app_blog"."i18n" # FROM "app_blog" # WHERE "app_blog"."title" = 'foo'
The text was updated successfully, but these errors were encountered:
jieter
changed the title
lazyness of the <field>_i18n lookup
non-lazyness of the <field>_i18n lookup transformation
Jan 25, 2018
Lookups are tranformed when the queryset definition is executed, which might be unexpected behavior with querysets.
An problematic use case might be defining the choices for a
ModelChoiceField
.Using a callable helps, but I'm not sure if the
queryset
argument of ModelChoiceField supports that:The text was updated successfully, but these errors were encountered: