django在使用'in'查找时的大小写

hfsqlsce  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(699)

我有一个模型:

class First(models.Model):
    first = models.IntegerField()
    second = models.IntegerField()

在我的心里 FirstView 我有一个复杂的过滤器 get_queryset 方法。

def get_queryset(self):
    CASE_FIRST = 1
    CASE_SECOND = 2

    return self.queryset.filter(~Q(first__in=[CASE_FIRST, CASE_SECOND]) |  # this line is ok 
                                   Q(second=Case(
                                       When(first=CASE_FIRST, then=(1, 2, 3))),
                                       When(first=CASE_SECOND, then=(3, 4, 5, 6))
                                       )
                                   )

但是这个代码是崩溃的,只有一个例外:

File "/home/path/to/project/venv/lib/python3.6/site-packages/rest_framework/viewsets.py", line 103, in view
    return self.dispatch(request, *args,**kwargs)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/rest_framework/views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/rest_framework/views.py", line 443, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/rest_framework/views.py", line 480, in dispatch
    response = handler(request, *args,**kwargs)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/rest_framework/mixins.py", line 42, in list
    page = self.paginate_queryset(queryset)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/rest_framework/generics.py", line 173, in paginate_queryset
    return self.paginator.paginate_queryset(queryset, self.request, view=self)
  File "/home/path/to/project/core/paginators.py", line 18, in paginate_queryset
    self.count = queryset.count()
  File "/home/path/to/project/venv/lib/python3.6/site-packages/django/db/models/query.py", line 387, in count
    return self.query.get_count(using=self.db)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 491, in get_count
    number = obj.get_aggregation(using, ['__count'])['__count']
  File "/home/path/to/project/venv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 476, in get_aggregation
    result = compiler.execute_sql(SINGLE)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1063, in execute_sql
    cursor.execute(sql, params)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/path/to/project/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 251, in execute
    self.errorhandler(self, exc, value)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/home/path/to/project/venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 412, in _query
    rowcount = self._do_query(q)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 375, in _do_query
    db.query(q)
  File "/home/path/to/project/venv/lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1241, 'Operand should contain 1 column(s)')

我怎样才能修理那次撞车?

agxfikkp

agxfikkp1#

你可以把这两种情况线性化。

return self.queryset.filter(
    ~Q(first__in[CASE_FIRST, CASE_SECOND]) |
    Q(first=CASE_FIRST, second__in=[1, 2, 3]) |
    Q(first=CASE_SECOND, second__in=[3, 4, 5, 6])
)

所以我们要把它分成三个条件:
这个 first 不在 [CASE_FIRST, CASE_SECOND] ,
这个 firstCASE_FIRST 以及 second[1, 2, 3] ; 和
这个 firstCASE_SECOND 以及 second[3, 4, 5, 6] .
所以我们把“暗示”转化为析取范式(dnf)。
举个例子 first=CASE_FIRST 那么第一个和最后一个案例永远不可能是真的,所以它需要 second 进入 [1, 2, 3] . 所以我们仍然有这个含义,因为我们接受外面的一切, CASE_FIRST 以及 CASE_SECOND ,以及 first 是一个 CASE_FIRST 或者 CASE_SECOND 因此,我们对 second 这需要得到满足。

相关问题