Django ORM:按额外属性筛选

2024-04-19 08:21:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过连接字符串筛选一些数据库对象。

正常的SQL查询是:

SELECT concat(firstName, ' ', name) FROM person WHERE CONCAT(firstName, ' ', name) LIKE "a%";

在模型中,我创建了一个名为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()

现在访问全名对单个对象有效:

>>> p = models.Person.objects.get(pk=4)
>>> p.fullName
u'Fred Borminski'

但它在过滤器中不起作用:

>>> 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, (...)

这是一个bug还是一个特性?我该怎么解决?

谢谢。


Tags: djangonameinpyselfdbmodelslib
3条回答

我通过实现自定义聚合函数解决了这个问题。 在这种情况下,我需要将各个字段连接到街道地址,以便能够筛选/搜索匹配项。 下面的聚合函数允许指定一个字段和一个或多个其他字段来执行SQL CONCAT。

2015年8月3日编辑:

https://stackoverflow.com/a/19529861/3230522收集细节的更好的实现。如果在子查询中使用queryset,则前面的实现将失败。表名现在是正确的,尽管我注意到这只适用于同一个表中的列的连接。

from django.db.models import Aggregate
from django.db.models.sql.aggregates import Aggregate as SQLAggregate

class SqlAggregate(SQLAggregate):
    sql_function = 'CONCAT_WS'
    sql_template = u'%(function)s(" ", %(field)s, %(columns_to_concatenate)s)'

    def as_sql(self, qn, connection):
        self.extra['columns_to_concatenate'] = ', '.join(
        ['.'.join([qn(self.col[0]), qn(c.strip())]) for c in self.extra['with_columns'].split(',')])
        return super(SqlAggregate, self).as_sql(qn, connection)

class Concatenate(Aggregate):
    sql = SqlAggregate

    def __init__(self, expression, **extra):
        super(Concatenate, self).__init__(
            expression,
            **extra)

    def add_to_query(self, query, alias, col, source, is_summary):

        aggregate = self.sql(col,
                         source=source,
                         is_summary=is_summary,
                         **self.extra)

        query.aggregates[alias] = aggregate

建议的解决方案对下面代码中的postgresql和JSONB字段非常有用。仅返回“key”jsonb字段下具有“partner”键的记录:

query_partner = "select key->>'partner' from accounting_subaccount " \
                "where accounting_subaccount.id = subaccount_id and key ? 'partner'"
qs = queryset.extra(select={'partner': query_partner}, where=["key ? 'partner'"])

不是虫子。filter()只检查模型定义,因此它不能将fullName识别为声明字段(因为它不是-它是查询中的额外参数)。

您可以使用extra()fullName添加到WHERE

Person.objects.extra(where=["fullName LIKE %s"], params=["Alexei%"])

相关问题 更多 >