简体   繁体   中英

reasons for serializer not validating data DRF

I am sending the data through postman as follows

在此处输入图像描述

my model.py is as follows

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)

my view.py is as follows

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)

My serializer.py is as follows

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

Now the problem is this that:

when I am posting data through Postman, in the View, i am getting data with both the keys ie 'album_data' which contains nested JSON data and 'file' which has list of uploaded files.

But i am not receiving the 'album_data' key in the validated_data of the create function in the serializer.

As i am new to DRF, i am unable to determine why data validation in the serializer is failing?

Here is the problem, on the ImageAlbumSerializer

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

The data is bieng passed from the request.data but u declared it as read_only field. so as the name implies that attr is a read_only so it wont be validated or even passed to the validate method. just remove that arg and it will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM