• <bdo id='QhP5r'></bdo><ul id='QhP5r'></ul>

    1. <tfoot id='QhP5r'></tfoot>
    2. <small id='QhP5r'></small><noframes id='QhP5r'>

    3. <i id='QhP5r'><tr id='QhP5r'><dt id='QhP5r'><q id='QhP5r'><span id='QhP5r'><b id='QhP5r'><form id='QhP5r'><ins id='QhP5r'></ins><ul id='QhP5r'></ul><sub id='QhP5r'></sub></form><legend id='QhP5r'></legend><bdo id='QhP5r'><pre id='QhP5r'><center id='QhP5r'></center></pre></bdo></b><th id='QhP5r'></th></span></q></dt></tr></i><div id='QhP5r'><tfoot id='QhP5r'></tfoot><dl id='QhP5r'><fieldset id='QhP5r'></fieldset></dl></div>
    4. <legend id='QhP5r'><style id='QhP5r'><dir id='QhP5r'><q id='QhP5r'></q></dir></style></legend>

        Django 模型翻译查询回退

        时间:2023-07-23

        <small id='w6k77'></small><noframes id='w6k77'>

          <tbody id='w6k77'></tbody>
        <i id='w6k77'><tr id='w6k77'><dt id='w6k77'><q id='w6k77'><span id='w6k77'><b id='w6k77'><form id='w6k77'><ins id='w6k77'></ins><ul id='w6k77'></ul><sub id='w6k77'></sub></form><legend id='w6k77'></legend><bdo id='w6k77'><pre id='w6k77'><center id='w6k77'></center></pre></bdo></b><th id='w6k77'></th></span></q></dt></tr></i><div id='w6k77'><tfoot id='w6k77'></tfoot><dl id='w6k77'><fieldset id='w6k77'></fieldset></dl></div>
        <tfoot id='w6k77'></tfoot>

          <legend id='w6k77'><style id='w6k77'><dir id='w6k77'><q id='w6k77'></q></dir></style></legend>

                • <bdo id='w6k77'></bdo><ul id='w6k77'></ul>
                • 本文介绍了Django 模型翻译查询回退的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在为多语言网站使用 django 模型翻译.

                  语言后备在直接读取属性时效果很好.例如,如果当前语言是德语并且我打印 object.title,如果没有定义德语标题,我将看到英文标题.

                  我希望回退也适用于查询,但事实并非如此.事实上,如果我做类似的事情

                  results = MyModel.objects.filter(title = 'hello')

                  如果未设置德语标题,这将不会得到任何结果,而我希望它返回带有英文标题hello"的对象.

                  我怎样才能做到这一点?

                  提前致谢.

                  解决方案

                  不幸的是,上面的答案是错误的.正确处理这种情况的方法是

                  <上一页>查询集 = 查询集.filter(Q(Q(Q(title_fr_fr__icontains=search) & ~Q(title_fr_fr=""))|Q(Q(title_en_gb__icontains=search) & Q(title_fr_fr=""))))

                  其中 fr-fr - 是我们现在使用的语言,而 en-gb - 是我们的默认语言.这意味着让我们获得目标语言标题包含可搜索字符串且目标语言标题为填充(非空)或默认语言标题包含可搜索字符串且目标语言标题为空的那些行.

                  所以整个示例将如下所示:

                  from django.db.models import Q###default_lang = settings.LANGUAGE_CODE.replace("-", "_")current_lang = get_language().replace("-", "_")查询集 = 查询集.filter(Q(Q(Q(**{f'title_{current_lang}__icontains': search}) & ~Q(**{f'title_{current_lang}': ""}))|Q(Q(**{f'title_{default_lang}__icontains': search}) & Q(**{f'title_{current_lang}': ""}))))

                  azuax 答案在某些情况下会给出错误的结果.假设 title_de = 'ABC',title_en = 'DEF' 和当前语言 de.对于搜索字符串DEF";我们得到这一行但不应该因为用户看到ABC"

                  I'm using django modeltranslation for a multi-language site.

                  Language fallback works good when reading attributes directly. For example, if current language is German and I print object.title, if the German title is not defined I'll see the english title.

                  I would expect fallback to work also on queries, but that's not true. In fact, if i do something like

                  results = MyModel.objects.filter(title = 'hello')
                  

                  this will get no results if the German title is not set, while I would like it to return the object with english title "hello".

                  How can I make this work?

                  Thanks in advance.

                  解决方案

                  Unfortunately answers above are wrong. The way to handle this case correct is

                      queryset = queryset.filter(
                          Q(
                              Q(Q(title_fr_fr__icontains=search) & ~Q(title_fr_fr=""))
                              |
                              Q(Q(title_en_gb__icontains=search) & Q(title_fr_fr=""))
                          )        
                      )
                  

                  Where fr-fr - is language we are working with now and en-gb - is our default language. This means get us those rows where title on target language contains searchable string AND title on target language is FILLED (not empty) OR where title on default language contains searchable string AND title on target language is empty.

                  So whole example will be looks like this:

                  from django.db.models import Q
                  ###
                  default_lang = settings.LANGUAGE_CODE.replace("-", "_")
                  current_lang = get_language().replace("-", "_")
                  
                  queryset = queryset.filter(
                      Q(
                          Q(Q(**{f'title_{current_lang}__icontains': search}) & ~Q(**{f'title_{current_lang}': ""}))
                          |
                          Q(Q(**{f'title_{default_lang}__icontains': search}) & Q(**{f'title_{current_lang}': ""}))
                          )
                      )
                  

                  azuax answer gives wrong results in some cases. Suppose title_de = 'ABC', title_en = 'DEF' and current language de. And for search string "DEF" we get this row but shouldn't because user see "ABC"

                  这篇关于Django 模型翻译查询回退的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ugettext 和 ugettext_lazy 下一篇:Python:在单词边界上拆分 unicode 字符串

                  相关文章

                • <tfoot id='AHxUf'></tfoot>
                  <i id='AHxUf'><tr id='AHxUf'><dt id='AHxUf'><q id='AHxUf'><span id='AHxUf'><b id='AHxUf'><form id='AHxUf'><ins id='AHxUf'></ins><ul id='AHxUf'></ul><sub id='AHxUf'></sub></form><legend id='AHxUf'></legend><bdo id='AHxUf'><pre id='AHxUf'><center id='AHxUf'></center></pre></bdo></b><th id='AHxUf'></th></span></q></dt></tr></i><div id='AHxUf'><tfoot id='AHxUf'></tfoot><dl id='AHxUf'><fieldset id='AHxUf'></fieldset></dl></div>
                  <legend id='AHxUf'><style id='AHxUf'><dir id='AHxUf'><q id='AHxUf'></q></dir></style></legend>

                  1. <small id='AHxUf'></small><noframes id='AHxUf'>

                        <bdo id='AHxUf'></bdo><ul id='AHxUf'></ul>