To pass data as a list inside a serializer in Django, you can create a ListField in your serializer and then pass the list data to it.

Here is an example:

from rest_framework import serializers

class MySerializer(serializers.Serializer):
    my_list = serializers.ListField(child=serializers.CharField())

# Example usage:
data = {'my_list': ['item1', 'item2', 'item3']}
serializer = MySerializer(data=data)
if serializer.is_valid():
    print(serializer.validated_data)

In this example, we created a serializer called MySerializer with a ListField called my_list. The ListField takes a child argument that specifies the type of data allowed in the list. In this case, we set it to CharField to accept a list of strings.

To pass the list data to the serializer, we create a dictionary with the data and pass it to the serializer's data argument. The is_valid() method is called to validate the data and return the validated data in the validated_data attribute of the serializer object.

how to pass data as list inside a serializer in django

原文地址: https://www.cveoy.top/t/topic/07n 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录