Reference: Query

This module contains the query utilities for the Translations app.

Important

The examples are assumed to CRUD this dataset.

Type\Lang

English

German

Continent

Europe

Europa

Asia

Asien

Country

Germany

Deutschland

South Korea

Südkorea

City

Cologne

Köln

Seoul

Seul

Please memorize this dataset in order to understand the examples better.

translations.query._fetch_translations_query_getter(model, lang)[source]

Return the translations query getter specialized for a model and some language(s).

Returns the function that can be used to convert lookups and queries of the model to their equivalent for searching the translations of that lookup or query in the specified language(s).

Parameters
  • model (type(Model)) – The model which the translations query getter is specialized for.

  • lang (str or list(str)) – The language(s) which the translations query getter is specialized for.

Returns

The translations query getter specialized for the model and the language(s).

Return type

function

To fetch the translations query getter specialized for a model and some language(s) (a custom language):

from translations.query import _fetch_translations_query_getter
from sample.models import Continent

getter = _fetch_translations_query_getter(Continent, 'de')
query = getter(countries__name__icontains='Deutsch')

# output
print(query)
(AND:
    (AND:
        ('countries__translations__field', 'name'),
        ('countries__translations__language', 'de'),
        ('countries__translations__text__icontains', 'Deutsch'),
    ),
)

To fetch the translations query getter specialized for a model and some language(s) (multiple custom languages):

from translations.query import _fetch_translations_query_getter
from sample.models import Continent

getter = _fetch_translations_query_getter(Continent, ['de', 'tr'])
query = getter(countries__name__icontains='Deutsch')

print(query)
(AND:
    (AND:
        ('countries__translations__field', 'name'),
        ('countries__translations__language__in', ['de', 'tr']),
        ('countries__translations__text__icontains', 'Deutsch'),
    ),
)
class translations.query.TQ[source]

Encapsulate translation queries as objects that can then be combined logically (using & and |).

Provides functionalities like __call__() to Specialize the TQ for some language(s) and _combine() to combine TQ objects logically with other Q objects.

To use TQ:

from translations.query import TQ
from sample.models import Continent

continents = Continent.objects.filter(
    TQ(
        countries__cities__name__startswith='Cologne',
    )         # use probe language (default English) for this query
    |         # logical combinator
    TQ(
        countries__cities__name__startswith='Köln',
    )('de')   # use German for this query
).distinct()

print(continents)
<TranslatableQuerySet [
    <Continent: Europe>,
]>
__init__(*args, **kwargs)[source]

Initialize a TQ with Q arguments.

This is an overriden version of the Q‘s __init__() method. It defines custom translation configurations on the TQ.

Parameters
  • args (list) – The arguments of the Q‘s __init__() method.

  • kwargs (dict) – The keyword arguments of the Q‘s __init__() method.

To Initialize a TQ:

from translations.query import TQ

tq = TQ(countries__cities__name__startswith='Köln')

print(tq)
(AND:
    ('countries__cities__name__startswith', 'Köln'),
)
__deepcopy__(memodict)[source]

Return a copy of the TQ object.

This is an overriden version of the Q‘s __deepcopy__() method. It copies the custom translation configurations from the current TQ to the copied TQ.

Parameters

memodict – The argument of the Q‘s __deepcopy__() method.

Returns

The copy of the TQ object.

Return type

TQ

To get a copy of a TQ object:

import copy
from translations.query import TQ

tq = TQ(countries__cities__name__startswith='Köln')('de')
cp = copy.deepcopy(tq)

print(cp)
print(cp.lang)
(AND:
    ('countries__cities__name__startswith', 'Köln'),
)
de
__call__(lang=None)[source]

Specialize the TQ for some language(s).

Causes the TQ to be queried in the specified language(s).

Parameters

lang (str or list or None) – The language(s) to specialize the query for. None means use the active language code.

Raises

ValueError – If the language code(s) is(are) not included in the LANGUAGES setting.

To specialize the TQ for some language(s):

from translations.query import TQ

tq = TQ(countries__cities__name__startswith='Köln')('de')

print(tq)
print(tq.lang)
(AND:
    ('countries__cities__name__startswith', 'Köln'),
)
de
_combine(other, conn)[source]

Return the result of logical combination with another Q object.

This is an overriden version of the Q‘s _combine() method. It combines the TQ object with another Q object logically.

Parameters
  • other (Q) – the other Q object.

  • conn (str) – The type of logical combination.

Returns

the result of logical combination with the other Q object.

Return type

Q

To get the result of logical combination with another Q object:

from translations.query import TQ

tq1 = TQ(countries__cities__name__startswith='Köln')('de')
tq2 = TQ(countries__cities__name__startswith='Koln')('tr')

print(tq1 | tq2)
(OR:
    (AND:
        ('countries__cities__name__startswith', 'Koln'),
    ),
    (AND:
        ('countries__cities__name__startswith', 'Köln'),
    ),
)