Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'ListSerializer' object has no attribute 'fields' #29

Closed
srtab opened this issue Apr 23, 2020 · 5 comments
Closed

AttributeError: 'ListSerializer' object has no attribute 'fields' #29

srtab opened this issue Apr 23, 2020 · 5 comments
Labels
bug Something isn't working

Comments

@srtab
Copy link

srtab commented Apr 23, 2020

Hi,
I'm using a ListSerializer in my views and when I try to load do openapi, just raise the following exception:

...
File "/usr/local/lib/python3.6/site-packages/drf_spectacular/views.py", line 34, in get
  public=spectacular_settings.SERVE_PUBLIC
File "/usr/local/lib/python3.6/site-packages/drf_spectacular/openapi.py", line 107, in get_schema
  paths=self.parse(None if public else request),
File "/usr/local/lib/python3.6/site-packages/drf_spectacular/openapi.py", line 87, in parse
  operation = view.schema.get_operation(path, method, self.registry)
File "/usr/local/lib/python3.6/site-packages/drf_spectacular/openapi.py", line 136, in get_operation
  request_body = self._get_request_body(path, method)
File "/usr/local/lib/python3.6/site-packages/drf_spectacular/openapi.py", line 743, in _get_request_body
  component = self.resolve_serializer(method, serializer)
File "/usr/local/lib/python3.6/site-packages/drf_spectacular/openapi.py", line 867, in resolve_serializer
  component.schema = self._map_serializer(method, serializer)
File "/usr/local/lib/python3.6/site-packages/drf_spectacular/openapi.py", line 605, in _map_serializer
  return self._map_basic_serializer(method, serializer)
File "/usr/local/lib/python3.6/site-packages/drf_spectacular/openapi.py", line 611, in _map_basic_serializer
  for field in serializer.fields.values():
AttributeError: 'ListSerializer' object has no attribute 'fields'

ListSerializer doesn't have fields attribute, but can be extracted from child attribute. Do you think that makes sense drf-spectacular to support this kind of serializers too?

@tfranzel tfranzel added the bug Something isn't working label Apr 24, 2020
@tfranzel
Copy link
Owner

Hi @srtab, thats for raising that issue. spectacular should never bail with an exception so this is a bug.

actually ListSerializers are usually auto-substituted by using field = XXXSerializer(many=True), which we support. I never used it manually.

could you please provide a small snipped on how you use ListSerializer, to maybe incorporate that use case?

@srtab
Copy link
Author

srtab commented Apr 24, 2020

I'm using it to make bulk updates, here is an simple example:

serializers.py

from rest_framework import serializers

from .models import Step


class StepSerializer(serializers.ModelSerializer):
    class Meta:
        model = Step
        fields = ("pk", "position")
        extra_kwargs = {"pk": {"read_only": False}}


class StepListSerializer(serializers.ListSerializer):
    child = StepSerializer()

    def update(self, instance, validated_data):
        ...

views.py

from rest_framework.generics import GenericAPIView

from .models import Step
from .serializers import StepListSerializer

class StepUpdateAPIView(GenericAPIView):
    model = Step
    serializer_class = StepListSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(self.get_queryset(), data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

@tfranzel
Copy link
Owner

@srtab that looks like reasonable usage. i fixed the exception and added ListSerializer handling for explicit usage.
the way the list is expanded (and the operation_id) is opinionated, but in accordance with the implicit usage. i hope this solves your problem.

@srtab
Copy link
Author

srtab commented Apr 24, 2020

@tfranzel That's perfect! Thanks for the fast response!
And Congrats for the fantastic app!

@tfranzel
Copy link
Owner

you're welcome and thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants