“Django Parler”;';语言代码';属性不能直接更改
我目前正在扩展一个插件,以便更好地使用Django翻译和语言。这里是正在讨论的方法。在涉及翻译之前,它根据需要工作 我扩展了查询,从几个翻译表中提取数据。然而,我得到了一个错误,我不知道如何处理“Django Parler”;';语言代码';属性不能直接更改,django,translation,django-cms,django-parler,django-translated-fields,Django,Translation,Django Cms,Django Parler,Django Translated Fields,我目前正在扩展一个插件,以便更好地使用Django翻译和语言。这里是正在讨论的方法。在涉及翻译之前,它根据需要工作 我扩展了查询,从几个翻译表中提取数据。然而,我得到了一个错误,我不知道如何处理 def get_authors_and_article_counts(self, authors): """method returning authors and their article counts"""
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)
我做错了什么?这里的问题是不是
raw\u authors=list(AuthorProfile.objects.raw(query))
?我应该做一些类似于MyModel.objects.language('en')代码>,更改查询后通过Parler运行查询以丢弃处理翻译的部分?正确的方法是什么?在我们继续讨论解决方案之前,让我们讨论一下发生此错误的原因
我们可以使用Model.objects.raw()
或直接执行自定义原始查询,以两种方式执行django原始查询。
当我们说:
Model.objects.raw()
⇾ 它基本上执行原始查询并返回模型实例
直接自定义原始查询⇾ 在这种情况下,您始终可以直接访问数据库,完全围绕模型层进行路由
对于您的案例列表(AuthorProfile.objects.raw(query))
在此过程中,它成功获取结果,但在尝试创建模型实例时出错
从您的回溯:文件“/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/
db/models/query.py”,第1368行,在迭代器中
setattr(实例、列、值[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 ....
回到你的答案上来。有两种方法可以遵循
只需从LanguageCodeDescriptor
方法\uuuuuu\uuuu
中删除异常即可。对django parler进行这样的更改当然不推荐
我想考虑一下你的建议:代码> MyMault.Objist.Load(EN)。您的查询似乎有点复杂。如果可能,请尝试使用ORM
,如果不可能,请遵循第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,那么您不能像下面这样访问模型实例author.article\u count
,因为此方法不返回模型实例
有关详细信息,请访问以下链接:
您是否使用了django翻译字段
?为什么要标记django翻译字段?@SpeedyMatch,我不知道那是什么。这似乎与此相关,因为,嗯,因为这个问题似乎与Django翻译字段有关。谢谢。那么,看来选项3真的是我唯一的选择了?如果不进行低层次的抽象,就没有办法做到这一点吗?我不知道django parler的很多细节,也许有一些方法可以有效地重写类方法(目前它会引发AttributeError)。您应该尝试选项2,我认为原始查询可以转换为ORM。查询中是否使用了不属于ORM的表?我认为所有使用的表要么直接与我拥有的特定模型相关,要么与它们的翻译相关。查询中没有其他类型的表。我一直在想如何通过ORM进行查询:/
from django.db import connection
with connection.cursor() as cursor:
cursor.execute(query, [published_clause, author_ids_tuple_str])
rows = cursor.fetchall()