I am trying to fulfill a bit of a complex request in my api for relaxing, so I can order my contacts in the correct order, now as e4c5 suggested in my previous question I could make this Case annotation and create my custom annotation with CASE / WHEN, and then use it in the annotations in order, but now I get ValueError at /api/sales/lead_contact/ The annotation 'status' conflicts with a field on the model , so this is the user annotation I'm trying to create, so I can order it correctly contacts, one note is that I I am opening this in the form of rest:
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
Model Fields:
class LeadContact(models.Model): status = models.CharField(max_length=10, choices=LeadContactConstants.STATUSES, default=LeadContactConstants.STATUS_PRISTINE)
and the choice for this field:
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"))
Serializer Class:
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
Full stack trace:
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.