django 串行器不验证数据DRF的原因

qpgpyjmq  于 2022-12-05  发布在  Go
关注(0)|答案(1)|浏览(152)

我通过 Postman 发送数据如下

我的model.py如下

def get_upload_path(instance, filename):
model = instance._meta
name = model.verbose_name_plural.replace(' ', '_')
return f'{name}/images/{filename}'

class ImageAlbum(models.Model):
def default(self):
    return self.images.filter(default=True).first()

def thumbnails(self):
    return self.images.filter(width__lt=100, length_lt=100)

class Photo(models.Model):
    name = models.CharField(max_length=255, null=True, blank=True)
    photo = models.ImageField(upload_to=get_upload_path, null=True, blank=True)
    default = models.BooleanField(default=False, null=True, blank=True)
    width = models.FloatField(default=100, null=True, blank=True)
    length = models.FloatField(default=100, null=True, blank=True)
    description = models.CharField(max_length=2000, null=True, blank=True)
    latitude = models.DecimalField(max_digits=11, decimal_places=2, null=True, blank=True)
    longitude = models.DecimalField(max_digits=11, decimal_places=2, null=True, blank=True)
    album = models.ForeignKey(ImageAlbum, related_name='album_data', on_delete=models.CASCADE, null=True, blank=True)

我的view.py如下

class ImageAlbumListApiView(APIView):
    permission_classes = [IsAuthenticated]
    parser_classes = [MultiPartParser, FormParser, ]

    def get(self, request):
        image_album = ImageAlbum.objects.all()
        serializer = ImageAlbumSerializer(image_album, many=True)
        return Response(serializer.data)
    
    def post(self, request):
        serializer = ImageAlbumSerializer(data=request.data)
        print(request.data)
        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(serializer.data)
        else:
            return Response(serializer.errors)

我的serializer.py如下

class PhotoSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Photo
        fields = '__all__'

class ImageAlbumSerializer(serializers.ModelSerializer):
    album_data = PhotoSerializer(many=True, read_only=True)
    file = serializers.ListField(
        child = serializers.ImageField(max_length = 1000000, allow_empty_file = False, use_url = False,
        write_only = True), write_only=True)
    
    class Meta:
        ###Test###
        model = models.ImageAlbum
        fields = ['id', 'album_data', 'file']
        read_only_fields = ['id']
    
    def create(self, validated_data):
        #album_data = validated_data.get('album_data')
        print(validated_data)
        uploaded_files = validated_data.get('file')
        #image_info = validated_data.pop('images')
        album = models.ImageAlbum.objects.create()
        for uploaded_item in uploaded_files:
            models.Photo.objects.get_or_create(album=album, photo=uploaded_item)
        return album

现在的问题是这样的:
当我通过Postman发布数据时,在视图中,我会同时获得两个键的数据,即包含嵌套JSON数据的“album_data”和包含已上载文件列表的“file”。
但是我没有在序列化程序的create函数的validated_data中接收到“album_data”键。
由于我是DRF的新手,因此无法确定串行器中的数据验证失败的原因?

j2cgzkjk

j2cgzkjk1#

问题就在这里,在ImageAlbumSerializer

...
    album_data = PhotoSerializer(many=True, read_only=True)
    ...

数据是从request.data传递过来的,但u将其声明为read_only字段。因此,正如名称所暗示的,attr是只读的,因此它不会被验证,甚至不会被传递给validate方法。

相关问题