Python django在顺序_上使用条件表达式的正确方法
我使用的是来自条件表达式示例文档,我在SO to order上找到了一些答案,通过Python django在顺序_上使用条件表达式的正确方法,python,django,django-models,django-rest-framework,Python,Django,Django Models,Django Rest Framework,我使用的是来自条件表达式示例文档,我在SO to order上找到了一些答案,通过status\u text或status,在这个示例中我使用了status\u text字段。我在我的apiget_queryset中做了所有这些,我做了类似的事情 def get_queryset(self): queryset = LeadContact.objects.none() user = self.request.user # I have some other conditi
status\u text
或status
,在这个示例中我使用了status\u text
字段。我在我的apiget_queryset
中做了所有这些,我做了类似的事情
def get_queryset(self):
queryset = LeadContact.objects.none()
user = self.request.user
# I have some other conditions here
# This is the conditional expressions that I'm building for order_by
case_sql = LeadContact.objects.annotate(
custom_order=Case(
When(status_text=LeadContactConstants.STATUS_CLIENT, then=Value(1)),
When(status_text=LeadContactConstants.STATUS_QUALIFIED, then=Value(2)),
When(status_text=LeadContactConstants.STATUS_CONTACTED, then=Value(3)),
When(status_text=LeadContactConstants.STATUS_PRISTINE, then=Value(4)),
output_field=CharField(),
)
).order_by('custom_order')
return queryset.extra(select={'status_text': case_sql}, order_by=['status_text'])
现在数据库正在抛出此回溯:
Traceback:
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/rest_framework/viewsets.py" in view
87. return self.dispatch(request, *args, **kwargs)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
466. response = self.handle_exception(exc)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
463. response = handler(request, *args, **kwargs)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/rest_framework/mixins.py" in list
42. page = self.paginate_queryset(queryset)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/rest_framework/generics.py" in paginate_queryset
172. return self.paginator.paginate_queryset(queryset, self.request, view=self)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/rest_framework/pagination.py" in paginate_queryset
303. return list(queryset[self.offset:self.offset + self.limit])
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
162. self._fetch_all()
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
965. self._result_cache = list(self.iterator())
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/db/models/query.py" in iterator
238. results = compiler.execute_sql()
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
840. cursor.execute(sql, params)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/db/utils.py" in __exit__
98. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py" in execute
124. return self.cursor.execute(query, args)
File "build/bdist.linux-x86_64/egg/MySQLdb/cursors.py" in execute
205. self.errorhandler(self, exc, value)
File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py" in defaulterrorhandler
36. raise errorclass, errorvalue
Exception Type: ProgrammingError at /api/sales/lead_contact/
Exception Value: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[<LeadContact: Liam Higgins>, <LeadContact: Juraj Polerecky>, <LeadContact: Pau' at line 1")
而LeadContactConstants
模型是:
class LeadContactConstants(object):
STATUS_PRISTINE = "PRISTINE"
STATUS_CONTACTED = "CONTACTED"
STATUS_QUALIFIED = "QUALIFIED"
STATUS_CLIENT = "CLIENT"
STATUSES = ((STATUS_PRISTINE, "Virgin"),
(STATUS_CONTACTED, "Contacted"),
(STATUS_QUALIFIED, "Qualified"),
(STATUS_CLIENT, "Client"))
我不知道如何处理这个问题,所以有人能告诉我发生了什么,我如何解决这个问题,这样我就可以正确地订购我的联系人了,谢谢。我不明白你为什么要把它传递给
.extra()
。这不是它的工作原理:case\u sql
是查询集本身,您应该返回它。我不明白您为什么要将它传递给.extra()
。这不是它的工作原理:case\u sql
是查询集本身,你应该直接返回它。好吧,我误解了文档中的一些内容,但是现在订单正在将我的状态从值变为数字,当我转到return.order\u by('case\u sql')
时,它仍然没有排序,我还做错了什么我会接受答案,但我仍在解决为什么这会将我的status\u text
更改为numberrok我误解了文档中的一些内容,但现在订单将我的status\u text
更改为值中的数字,它仍然没有排序,当我转到“return.order\u by('case\u sql')
时,我在这里还做错了什么我会接受答案,但我仍在解决为什么这会将我的状态\u text
更改为数字
class LeadContactConstants(object):
STATUS_PRISTINE = "PRISTINE"
STATUS_CONTACTED = "CONTACTED"
STATUS_QUALIFIED = "QUALIFIED"
STATUS_CLIENT = "CLIENT"
STATUSES = ((STATUS_PRISTINE, "Virgin"),
(STATUS_CONTACTED, "Contacted"),
(STATUS_QUALIFIED, "Qualified"),
(STATUS_CLIENT, "Client"))