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:
- 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- TQfor some language(s) and- _combine()to combine- TQobjects logically with other- Qobjects.- 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 - TQwith- Qarguments.- This is an overriden version of the - Q‘s- __init__()method. It defines custom translation configurations on the- TQ.- Parameters:
 - 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 - TQobject.- This is an overriden version of the - Q‘s- __deepcopy__()method. It copies the custom translation configurations from the current- TQto the copied- TQ.- Parameters:
- memodict – The argument of the - Q‘s- __deepcopy__()method.
- Returns:
- The copy of the - TQobject.
- Return type:
 - To get a copy of a - TQobject:- 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 - TQfor some language(s).- Causes the - TQto be queried in the specified language(s).- Parameters:
- lang (str or list or None) – The language(s) to specialize the query for. - Nonemeans use the active language code.
- Raises:
- ValueError – If the language code(s) is(are) not included in the - LANGUAGESsetting.
 - To specialize the - TQfor 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 - Qobject.- This is an overriden version of the - Q‘s- _combine()method. It combines the- TQobject with another- Qobject logically.- Parameters:
- Returns:
- the result of logical combination with the other - Qobject.
- Return type:
 - To get the result of logical combination with another - Qobject:- 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'), ), )
 
