From: Radek Czajka Date: Thu, 10 Jan 2013 14:22:13 +0000 (+0100) Subject: add tQ for filtering on translated fields X-Git-Tag: 0.1.5~3 X-Git-Url: https://git.mdrn.pl/fnpdjango.git/commitdiff_plain/292cabe349805d18e8a6d8bea10321ee48a3a5ff?hp=1fc5858c6fb90622900aeb2e2102a51d299c305b add tQ for filtering on translated fields --- diff --git a/fnpdjango/utils/models/translation.py b/fnpdjango/utils/models/translation.py index dd1d515..de4fe92 100644 --- a/fnpdjango/utils/models/translation.py +++ b/fnpdjango/utils/models/translation.py @@ -4,15 +4,23 @@ Utilities for creating multilingual fields in your apps. from copy import copy from django.conf import settings +from django.db import models from django.utils.translation import get_language, string_concat +def localize_field(name, lang=None): + if lang is None: + lang = get_language() + if lang not in (x[0] for x in settings.LANGUAGES): + lang = settings.LANGUAGE_CODE + return "%s_%s" % (name, lang) + def field_getter(name): @property def getter(self): - val = getattr(self, "%s_%s" % (name, get_language()), None) + val = getattr(self, localize_field(name), None) if not val: - val = getattr(self, "%s_%s" % (name, settings.LANGUAGE_CODE)) + val = getattr(self, localize_field(name, settings.LANGUAGE_CODE)) return val return getter @@ -26,7 +34,7 @@ def add_translatable(model, fields, languages=None): new_field = copy(field) if field.verbose_name: new_field.verbose_name = string_concat(field.verbose_name, ' [%s]' % lang_code) - new_field.contribute_to_class(model, "%s_%s" % (name, lang_code)) + new_field.contribute_to_class(model, localize_field(name, lang_code)) setattr(model, name, field_getter(name)) # add setter? @@ -38,12 +46,12 @@ def add_translatable_index(index_class, fields, languages=None): for name, field in fields.items(): for lang_code, lang_name in languages: new_field = copy(field) - fname = "%s_%s" % (name, lang_code) + fname = localize_field(name, lang_code) new_field.index_fieldname = new_field.index_fieldname \ - and "%s_%s" % (new_field.index_fieldname, lang_code) \ + and localize_field(new_field.index_fieldname, lang_code) \ or fname new_field.model_attr = new_field.model_attr \ - and "%s_%s" % (new_field.model_attr, lang_code) \ + and localize_field(new_field.model_attr, lang_code) \ or fname setattr(index_class, fname, new_field) index_class.fields[fname] = new_field @@ -51,7 +59,14 @@ def add_translatable_index(index_class, fields, languages=None): def translated_fields(field_names, languages=settings.LANGUAGES): """Generate a tuple of field names in translated versions.""" - return tuple("%s_%s" % (field_name, lang_code) + return tuple(localize_field(field_name, lang_code) for field_name in field_names for lang_code, lang_name in languages ) + +def tQ(**kwargs): + """ Creates a query (Q) with lookups on translated fields. """ + trans_kwargs = {} + for k, v in kwargs.items(): + trans_kwargs[localize_field(k)] = v + return models.Q(**trans_kwargs) diff --git a/setup.py b/setup.py index 17230c9..87a5a5e 100755 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ def whole_trees(package_dir, paths): setup( name='fnpdjango', - version='0.1.3', + version='0.1.4', author='Radek Czajka', author_email='radoslaw.czajka@nowoczesnapolska.org.pl', url = '',