【发布时间】:2018-12-15 20:00:33
【问题描述】:
我现在正在学习 DRF,我对这个 many = True 代码有点困惑。它有什么作用?或者是什么意思?
示例 1
class AlbumSerializer(serializers.ModelSerializer):
tracks = serializers.RelatedField(many=True)
class Meta:
model = Album
fields = ('album_name', 'artist', 'tracks')
示例 2
class UserList(generics.ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = (IsAdminUser,)
def list(self, request):
# Note the use of `get_queryset()` instead of `self.queryset`
queryset = self.get_queryset()
serializer = UserSerializer(queryset, many=True)
return Response(serializer.data)
【问题讨论】:
-
@DanielRoseman 是的先生,读过它,这就是为什么我在序列化器关系中感到困惑,它写成
many - If applied to a to-many relationship, you should set this argument to True.,然后在序列化器中写成To serialize a queryset or list of objects instead of a single object instance, you should pass the many=True... 一个代码,但两种不同的解释让我困惑.. -
这些解释有什么不同?两者都说如果您有多个对象,请将其设置为 true。
标签: django django-rest-framework