ValueError - 注释“状态”与模型 django 上的字段冲突

2024-03-30

我正在尝试在其余 api 视图中执行一些复杂的查询,以便我可以按正确的顺序对联系人进行排序,现在为e4c5 https://stackoverflow.com/a/43781018/4107823在我之前的问题中建议我可以这样做Case https://docs.djangoproject.com/en/1.8/ref/models/conditional-expressions/#case注释并使用 CASE/WHEN 构建自定义注释,然后按顺序在注释中使用它,但现在我得到了ValueError at /api/sales/lead_contact/ The annotation 'status' conflicts with a field on the model所以这是我正在尝试构建的自定义注释,以便我可以正确排序联系人,需要注意的是我正在休息视图中执行此操作:

   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.

  1. 正如您可以看到错误消息“‘status’与模型上的字段冲突”, 这里的错误告诉您 LeadContact 模型已经具有字段状态(您可以在 LeadContact 的模型定义中看到它) 这就是为什么你无法注释。
  2. 基本上,注释尝试在您的情况下向您的查询集结果添加一个字段status因为您有一个模型 LeadContactstatus您无法对其进行注释的字段。
  3. 解决方案是您必须使用字段名称,而不是状态和选择。 希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ValueError - 注释“状态”与模型 django 上的字段冲突 的相关文章

随机推荐