如何以字符串形式执行Django查询

时间:2018-09-19 16:48:25

标签: django django-models

我想像这样在Django ORM中进行查询:

query_string = "optionA=True,optionB=True,optionC=True"
data = ModelObject.objects.filter(query_string)

此代码给出以下错误:

Traceback (most recent call last):
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/rest_framework/views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/rest_framework/views.py", line 443, in handle_exception
self.raise_uncaught_exception(exc)
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/rest_framework/views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "/root/hellomeets/hellomeets/frontend/views.py", line 74, in post
filtered_emails = HelloMeetsMailList.objects.filter(final_query)
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/django/db/models/query.py", line 784, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/django/db/models/query.py", line 802, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1250, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1276, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/root/hellomeets/.env/hellomeets/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1151, in build_filter
arg, value = filter_expr
ValueError: too many values to unpack

我也尝试了eval(),但是没有用。 有什么办法让我不需要写原始的SQL查询

2 个答案:

答案 0 :(得分:2)

from django.db.models import Q

query_string=Q(optionA=True) & Q(optionB=True) & Q(optionC=True)
data = ModelObject.objects.filter(query_string)

有关更多详细信息: https://docs.djangoproject.com/en/2.1/topics/db/queries/#complex-lookups-with-q-objects

答案 1 :(得分:1)

如果您确实需要使用该字符串(这似乎是查询Django模型的一种非常笨拙的方式),则可以拆分并将其转换为dict:

s = "optionA=True,optionB=True,optionC=True"
kwargs = {}
for e in s.split(','):
    k, v = e.split('=')
    kwargs[k] = bool(v)

,然后在filter()方法中将该字典解压缩

data = ModelObject.objects.filter(**kwargs)

但这不是真的推荐!!