Django ORM:按附加属性过滤 [英] Django ORM: Filter by extra attribute
问题描述
我想通过连接字符串过滤一些数据库对象。
正常的SQL查询将是:
SELECT concat(firstName,'',name)FROM person WHERE CONCAT(firstName,'',name)LIKEa%;
在模型中,我创建了一个名为PersonObjects的管理器:
class PersonObjects(Manager):
attrs = {
'fullName':CONCAT(firstName,'',name)
def get_query_set(self):
return super(PersonObjects,self).get_query_set()。extra(
select = self.attrs)
我还在我的模型中配置了这个:
objects = managers.PersonObjects()
现在访问fullName可用于单个对象: / p>
>>> p = models.Person.objects.get(pk = 4)
>>>> p.fullName
u'Fred Borminski'
但它不能在过滤器中工作:
>>> p = models.Person.objects.filter(fullName__startswith ='Alexei')
回溯(最近一次调用最后):
在< module>中的文件< console&
文件/usr/lib/python2.7/site-packages/django/db/models/manager.py,第141行,在过滤器
中return self.get_query_set()。filter(* args ,** kwargs)
文件/usr/lib/python2.7/site-packages/django/db/models/query.py,第550行,在过滤器
中return self._filter_or_exclude(False ,* args,** kwargs)
文件/usr/lib/python2.7/site-packages/django/db/models/query.py,第568行,位于_filter_or_exclude
clone.query .add_q(Q(* args,** kwargs))
文件/usr/lib/python2.7/site-packages/django/db/models/sql/query.py,第1128行,位于add_q
can_reuse = used_aliases)
文件/usr/lib/python2.7/site-packages/django/db/models/sql/query.py,行1026,在add_filter中
negate = negate,process_extras = process_extras)
文件/usr/lib/python2.7/site-packages/django/db/models/sql/query.py,第1191行,位于setup_joins
Choices are:%s%(name,,。.join(names)))
FieldError:无法将关键字'fullName'解析到字段中。选项是:firstName,gender,name,(...)
这是一个错误或功能?如何解决此问题?
谢谢。
解决方案一个bug。 filter()
仅检查模型定义,因此不能将 fullName
识别为声明字段(因为它不是 - 它是查询中的一个额外的参数)。
您可以使用将 fullName
添加到 WHERE
code> extra()
:
Person.objects.extra fullName LIKE%s],params = [Alexei%])
I want to filter some database objects by a concatenated string.
The normal SQL query would be:
SELECT concat(firstName, ' ', name) FROM person WHERE CONCAT(firstName, ' ', name) LIKE "a%";
In the model, I have created a manager called PersonObjects:
class PersonObjects(Manager):
attrs = {
'fullName': "CONCAT(firstName, ' ', name)"
}
def get_query_set(self):
return super(PersonObjects, self).get_query_set().extra(
select=self.attrs)
I also configured this in my model:
objects = managers.PersonObjects()
Now accessing fullName works for single objects:
>>> p = models.Person.objects.get(pk=4)
>>> p.fullName
u'Fred Borminski'
But it does not work in a filter:
>>> p = models.Person.objects.filter(fullName__startswith='Alexei')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/django/db/models/manager.py", line 141, in filter
return self.get_query_set().filter(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 550, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 568, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1128, in add_q
can_reuse=used_aliases)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1026, in add_filter
negate=negate, process_extras=process_extras)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1191, in setup_joins
"Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'fullName' into field. Choices are: firstName, gender, name, (...)
Is this a bug or a feature? How can I fix this?
Thanks.
解决方案 It's not a bug. filter()
only inspects model definitions, so it doesn't recognize fullName
as a declared field (because it's not - it's an extra argument in a query).
You can add the fullName
to WHERE
using extra()
:
Person.objects.extra(where=["fullName LIKE %s"], params=["Alexei%"])
这篇关于Django ORM:按附加属性过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!