ojii / django-multilingual-ng

THIS PROJECT IS *NOT* SUPPORTED AND SHOULD NOT BE USED UNLESS YOU KNOW EXACTLY WHAT YOU'RE DOING!!!

Home Page:https://github.com/KristianOellegaard/django-hvad

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get None if no translation

Natim opened this issue · comments

Hello,

I am looking for this :

menus = Section.objects.select_related().exclude(titleTop__exact='').filter(level__exact=0).filter(menuTop=True).order_by('orderTop')

And I am expecting that titleTop__exact='' exclude the Section without translation in the current language_code of the user.

It was working before but now ? How should I do this ?

filters will always try to fall back.

You have two options, either do the queryset in a way that you query the translation model directly or use the global language lock.

How can I use the global language lock ?

in multilingual/util.py you have:

class GlobalLanguageLock(object):

this class have lock and release, what is more its already initilaised so your code may look as follows:

from multilingual.utils import GLL

GLL.lock('fi')

// some code

GLL.release()

Btw.

I'm wondering, if it would be very hard or if there is a simple way to achieve following:

menus = Section.objects.select_related().exclude(titleTop__exact='', related_translation='pl').order_by('orderTop')

Special keyword related_translation that will search for object in polish variant, without looking at global language.

So far I did something like this :

    from multilingual.languages import get_translated_field_alias

    if language_code is None:
        language_code = get_language()

    parents = self.get_ancestors()
    items = []

    field_alias = get_translated_field_alias('slug', language_code)

    for a in parents:
        a.slug = getattr(a, field_alias, '')

But with the GlobalLanguageLock it will be easier to make request.
Thanks