0

我目前正在扩展一个插件以与 Django 翻译和语言一起使用。这是有问题的方法。在涉及翻译之前,它根据需要工作。

我扩展了查询以从几个翻译表中提取数据。但是,我收到一个错误,我不确定如何处理。

    def get_authors_and_article_counts(self, authors):
        """method returning authors and their article counts"""

        # first, we collect ids of authors for which we need to get data
        author_ids = [author.id for author in self.authors.all()]
        author_ids_tuple_str = '(' +  str(author_ids).strip('[]') + ')'

        #limit subquery to published articles
        published_clause = """ AND
            is_published %s AND
            publishing_date <= %s
            """ % (SQL_IS_TRUE, SQL_NOW_FUNC, )

        query = """
                with article_count as (
                  select author_id, count(*) as article_count
                    from aldryn_newsblog_article
                   where app_config_id = 1
                    %s
                   group by author_id
                )

                select distinct prof.*, coalesce(ac.article_count, 0) as article_count, author_trans.*, aldryn_people_trans.slug
                from common_authorprofile prof

                left join article_count ac
                on ac.author_id = prof.profile_id

                left join common_authorprofile_translation author_trans
                on prof.id = author_trans.master_id

                left join aldryn_people_person_translation aldryn_people_trans
                on prof.profile_id = aldryn_people_trans.master_id
                WHERE
                        prof.id IN %s AND
                        author_trans.language_code = 'ru';

""" % (published_clause, author_ids_tuple_str)

        print(query)
        #print(author_ids)

        raw_authors = list(AuthorProfile.objects.raw(query))
        #print(raw_authors)
        authors = [author for author in raw_authors if author.article_count]

        print(authors)
        return sorted(authors, key=lambda x: x.article_count, reverse=True)

追溯:

Traceback (most recent call last):
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
core/handlers/exception.py", line 34, in inner                                          
    response = get_response(request)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
core/handlers/base.py", line 156, in _get_response                                      
    response = self.process_exception_by_middleware(e, request)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
core/handlers/base.py", line 154, in _get_response                                      
    response = response.render()
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
template/response.py", line 106, in render                                              
    self.content = self.rendered_content
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
template/response.py", line 83, in rendered_content                                     
    content = template.render(context, self._request)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
template/backends/django.py", line 61, in render                                        
    return self.template.render(context)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
template/base.py", line 171, in render                                                  
    return self._render(context)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
template/base.py", line 163, in _render                                                 
    return self.nodelist.render(context)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
template/base.py", line 937, in render                                                  
    bit = node.render_annotated(context)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
template/base.py", line 904, in render_annotated                                        
    return self.render(context)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/classyt
ags/core.py", line 153, in render                                                       
    return self.render_tag(context, **kwargs)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/sekizai
/templatetags/sekizai_tags.py", line 93, in render_tag                                  
    rendered_contents = nodelist.render(context)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
template/base.py", line 937, in render                                                  
    bit = node.render_annotated(context)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
template/base.py", line 904, in render_annotated                                        
    return self.render(context)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/classyt
ags/core.py", line 153, in render                                                       
    return self.render_tag(context, **kwargs)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/cms/tem
platetags/cms_tags.py", line 447, in render_tag                                         
    return toolbar.render_with_structure(context, nodelist)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/cms/too
lbar/toolbar.py", line 477, in render_with_structure                                    
    rendered_contents = nodelist.render(context)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
template/base.py", line 937, in render                                                  
    bit = node.render_annotated(context)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
template/base.py", line 904, in render_annotated                                        
    return self.render(context)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/classyt
ags/core.py", line 153, in render                                                       
    return self.render_tag(context, **kwargs)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/cms/tem
platetags/cms_tags.py", line 313, in render_tag                                         
    nodelist=nodelist,
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/cms/plu
gin_rendering.py", line 343, in render_page_placeholder                                 
    nodelist=None,
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/cms/plu
gin_rendering.py", line 259, in render_placeholder                                      
    placeholder_content = ''.join(plugin_content)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/cms/plu
gin_rendering.py", line 456, in render_plugins                                          
    yield self.render_plugin(plugin, context, placeholder, editable)
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/cms/plu
gin_rendering.py", line 429, in render_plugin                                           
    context = plugin.render(context, instance, placeholder.slot)
  File "/home/user/sites/app-web/app/apps/plugins/cms_plugins.py", line 1
8, in render                                                                            
    authors_list = instance.get_authors_and_article_counts(request)
  File "/home/user/sites/app-web/app/apps/plugins/models.py", line 88, in
 get_authors_and_article_counts                                                         
    raw_authors = list(AuthorProfile.objects.raw(query))
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
db/models/query.py", line 1339, in __iter__                                             
    self._fetch_all()
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
db/models/query.py", line 1326, in _fetch_all                                           
    self._result_cache = list(self.iterator())
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
db/models/query.py", line 1368, in iterator                                             
    setattr(instance, column, values[pos])
  File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/parler/
fields.py", line 161, in __set__                                                        
    raise AttributeError("The 'language_code' attribute cannot be changed directly! Use t
he set_current_language() method instead.")                                             
AttributeError: The 'language_code' attribute cannot be changed directly! Use the set_cur
rent_language() method instead.       

我究竟做错了什么?问题出在这里raw_authors = list(AuthorProfile.objects.raw(query))吗?我是否应该做类似的事情MyModel.objects.language('en'). ...,在更改查询以丢弃处理翻译的部分后通过 Parler 运行查询?什么是正确的方法?

4

1 回答 1

1

在我们继续解决之前,让我们讨论一下为什么会发生这个错误。

我们可以使用Model.objects.raw()直接执行自定义原始查询以两种方式执行 django 原始查询。当我们说:

  1. Model.objects.raw()⇾ 它基本上执行原始查询并返回模型实例。
  2. 直接自定义原始查询⇾在这种情况下,您始终可以直接访问数据库,完全围绕模型层进行路由。

对于您list(AuthorProfile.objects.raw(query))在此期间的情况,它成功获取结果,但是当它尝试制作时会发生错误model instance....

从您的回溯中:文件“/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/models/query.py”,第 1368 行,在迭代器中
setattr(instance, column, values[pos])

...因为这个实例不允许设置属性。请参阅下面的 django-parler代码库

class LanguageCodeDescriptor(object):
    ..... some code ....
    def __set__(self, instance, value):
        raise AttributeError("The 'language_code' attribute cannot be changed directly! Use the set_current_language() method instead.")
    ..... some code ....

回到你的答案。您可以遵循几种方法。

  1. LanguageCodeDescriptor只需从方法中删除异常__set__。像这样在 django-parler 中进行更改当然不建议这样做。

  2. 我想考虑你的建议MyModel.objects.language('en')。您的查询似乎有点复杂,如果可能,请尝试使用ORM,如果不可能,请遵循第 3 点。

  3. 使用上面讨论的方法 2 执行原始查询,即直接自定义原始查询,

    from django.db import connection
    with connection.cursor() as cursor:
       cursor.execute(query, [published_clause, author_ids_tuple_str])
       rows = cursor.fetchall()    
    

注意:如果您使用方法 3,那么您不能像这样访问 model_instance,author.article_count因为此方法不返回模型实例。

可以通过以下链接了解更多详情:

执行原始 sql

django-parler 代码库

于 2020-08-08T19:31:34.287 回答