I have a DRF API. It is throwing an error that I have not been able to fix.

Here is my view and serializer:

class CreateNotations(CreateAPIView):
    permission_classes = [IsAuthenticated]
    serializer_class = NotationListSerializer

    def post(self, request, *args, **kwargs):
        return super().post(request, *args, **kwargs)

class NotationListSerializer(serializers.Serializer):
    workitem_id =
serializers.PrimaryKeyRelatedField(queryset=WorkItem.objects.all(),
source="workitem")
    notations = NotationSerializer(many=True)

    def create(self, validated_data):
        workitem = validated_data["workitem"]

        IncompleteAnalysis.objects.filter(workitem=workitem).delete()

        data = []
        for notation in validated_data["notations"]:
            data.append({
                'workitem': workitem,
                'incomplete_analysis_reason': notation["reason"],
                'vessel_affected': notation["vessel"],
                'user': self.context['request'].user,
                'timestamp': datetime.now().isoformat()
            })

        return
IncompleteAnalysis.objects.bulk_create([IncompleteAnalysis(**d) for d in
data])

The body of the request received is:

{'workitem_id': 244, 'notations': [{'reason_id": 4, 'vessel_id': 6}]}

It fails with this:

AttributeError: Got AttributeError when attempting to get a value for field
`workitem_id` on serializer `NotationListSerializer`.
The serializer field might be named incorrectly and not match any attribute
or key on the `list` instance.
Original exception text was: 'list' object has no attribute 'workitem'.

Here is the stack trace:

ERROR    django.request:log.py:241 Internal Server Error: /api/notations/
Traceback (most recent call last):
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/fields.py",
line 446, in get_attribute
    return get_attribute(instance, self.source_attrs)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/fields.py",
line 96, in get_attribute
    instance = getattr(instance, attr)
AttributeError: 'list' object has no attribute 'workitem'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/django/core/handlers/exception.py",
line 56, in inner
    response = get_response(request)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/django/core/handlers/base.py",
line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/django/views/decorators/csrf.py",
line 55, in wrapped_view
    return view_func(*args, **kwargs)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/django/views/generic/base.py",
line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/views.py",
line 509, in dispatch
    response = self.handle_exception(exc)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/views.py",
line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/views.py",
line 480, in raise_uncaught_exception
    raise exc
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/views.py",
line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/inst/martell/EVServer/app/CAPgraph/createevapi/views.py", line
1270, in post
    return super().post(request, *args, **kwargs)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/generics.py",
line 190, in post
    return self.create(request, *args, **kwargs)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/mixins.py",
line 20, in create
    headers = self.get_success_headers(serializer.data)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/serializers.py",
line 555, in data
    ret = super().data
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/serializers.py",
line 253, in data
    self._data = self.to_representation(self.instance)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/serializers.py",
line 509, in to_representation
    attribute = field.get_attribute(instance)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/relations.py",
line 190, in get_attribute
    return super().get_attribute(instance)
  File
"/home/larrymartell/.local/lib/python3.8/site-packages/rest_framework/fields.py",
line 479, in get_attribute
    raise type(exc)(msg)
AttributeError: Got AttributeError when attempting to get a value for field
`workitem_id` on serializer `NotationListSerializer`.
The serializer field might be named incorrectly and not match any attribute
or key on the `list` instance.
Original exception text was: 'list' object has no attribute 'workitem'.

This is vexing as I have other very similar APIs that work fine.

Thanks!
Larry

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACwCsY4cLuyRjLAu_xx28q_rssra7WowocevocnqgzZuu%3DnVew%40mail.gmail.com.

Reply via email to