Add LocalizedRef expression for extracting the value in the current language

This commit is contained in:
Swen Kooij
2017-05-30 13:38:27 +03:00
parent e5214b07ae
commit 06f7ee15f0
3 changed files with 96 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
from django.conf import settings
from django.utils import translation
from psqlextra import expressions
class LocalizedRef(expressions.HStoreRef):
"""Expression that selects the value in a field only in
the currently active language."""
def __init__(self, name: str, lang: str=None):
"""Initializes a new instance of :see:LocalizedRef.
Arguments:
name:
The field/column to select from.
lang:
The language to get the field/column in.
If not specified, the currently active language
is used.
"""
language = lang or translation.get_language() or settings.LANGUAGE_CODE
super().__init__(name, language)