ValueError-注释“状态”与模型django上的字段冲突 [英] ValueError - The annotation 'status' conflicts with a field on the model django
问题描述
我正在尝试在其余api视图中执行一些复杂的查询,以便我可以按正确的顺序对联系人进行排序,现在为 e4c5 在我之前的问题中提出,我可以这样做案例批注,并使用CASE / WHEN构建我的自定义批注,然后按顺序在批注中使用该批注,但现在我在/ api / sales / lead_contact /中遇到 ValueError
,所以这是我要构建的自定义注释,因此我可以正确地订购联系人,请注意,我正在执行在其余视图中:
注释状态与模型中的字段冲突
I'm trying to perform a bit of complicated query in my rest api view so I can order my contacts in the right order, now as e4c5 suggested in my previous question I could do this Case annotation and build my custom annotation with CASE/WHEN and then use that in annotation in the order by, but now I'm getting ValueError at /api/sales/lead_contact/
The annotation 'status' conflicts with a field on the model
so this is the custom annotation I'm trying to build so I can properly order contacts, one note is that I'm preforming this in rest view:
class LeadContactViewSet(viewsets.ModelViewSet):
def get_queryset(self):
filter_date = self.request.query_params.get('filter_date', None)
case_sql = LeadContact.objects.annotate(
status=Case(
When(status=LeadContactConstants.STATUS_CLIENT, then=Value('1')),
When(status=LeadContactConstants.STATUS_QUALIFIED, then=Value('2')),
When(status=LeadContactConstants.STATUS_CONTACTED, then=Value('3')),
When(status=LeadContactConstants.STATUS_PRISTINE, then=Value('4')),
default=Value('1'),
output_field=CharField(),
)
).values_list('status')
if filter_date is not None:
queryset = queryset.filter(next_action_date=filter_date).extra(select={'status': case_sql},
order_by=['status'])
return queryset
模型字段:
class LeadContact(models.Model):
status = models.CharField(max_length=10,
choices=LeadContactConstants.STATUSES, default=LeadContactConstants.STATUS_PRISTINE)
以及该字段的选择:
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"))
序列化器类:
class LeadContactSerializer(serializers.ModelSerializer):
account_handler = AccountHandlerSerializer()
next_action_date = serializers.DateTimeField(format=settings.CUSTOM_DATE_FORMAT_NO_TIME)
absolute_url = serializers.URLField(source='get_absolute_url')
class Meta:
model = LeadContact
fields = (
'pk', 'organization_name', 'sub_organization_name', 'serial_number', 'account_handler', 'status_text',
'first_name', 'last_name', 'next_action_date', 'absolute_url', 'status_display_class'
)
depth = 1
完整堆栈跟踪:
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
40. queryset = self.filter_queryset(self.get_queryset())
File "/home/vagrant/vincluos/VincluCMSProject/vinclucms_sales/restapi/views/lead_contact_viewset.py" in get_queryset
29. output_field=CharField(),
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
127. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/vagrant/virtualenv/local/lib/python2.7/site-packages/django/db/models/query.py" in annotate
793. "the model." % alias)
Exception Type: ValueError at /api/sales/lead_contact/
Exception Value: The annotation 'status' conflicts with a field on the model.
推荐答案
- 您可以阅读错误消息'状态'与模型上的字段冲突,
,这里的错误告诉您LeadContact模型已经具有字段状态(您可以在LeadContact的模型定义中看到它)
这就是为什么您无法注释的原因。 - 基本注释会尝试将字段添加到查询集结果中(如果您使用的是状态,该字段为状态) LeadContact和 status 字段,因此您无法对其进行注释。
- 解决方案是您必须输入字段名称,然后再选择状态和选择。
希望这会有所帮助。
- As you can read the error message "'status' conflicts with a field on the model", here the error is telling you that LeadContact model already has a field status (you can see it on your model definition of LeadContact) that's why you're not able to annotate.
- Basically annotate tries to add a field to your queryset result in your case it is status as you have a model LeadContact with status field because of which you're not able to annotate.
- The Solution is you have to the field name other then status and choices. hope this helps.
这篇关于ValueError-注释“状态”与模型django上的字段冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!