E
E
exctac2018-08-30 10:33:51
Django
exctac, 2018-08-30 10:33:51

Django, how to pass a list of numbers in a GET request and validate their serializers.ListField?

Hello!
I want to send a list of numbers to the view with a GET request, validate this list with a serializer, but something goes wrong, I don’t understand in what format to transfer the list to GET, this kind {'panels': [1,2,3]}does not work, since it perceives the list as a string "[1,2,3 ]". And throws an error

{'panels': {0: [ErrorDetail(string='Требуется целочисленное значение.', code='invalid')]}}
If I pass a number, {'panels': 1}then everything is ok, but I need to transfer the list.
class PanelsSerializer(rest_serializers.Serializer):
     panels = rest_serializers.ListField(
        required=True,
        allow_empty=False,
        allow_null=False,
        child=rest_serializers.IntegerField()
    )

class Panels(APIView):
    def get(self, request, **kwargs):
        ps = PanelsSerializer(data=request.GET)
        ps.is_valid()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
exctac, 2018-08-30
@exctac

I remembered how arrays are passed in the form:
{'panels[1]': 1, 'panels[2]': 2, 'panels[3]': 3}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question