Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

django - getting a huge null list on response while trying to upload through DRF

I am getting error while trying to upload files through POSTMAN here is the error and it is 26,000+ long. Although the uploaded files are being uploaded to the directory just fine but the return response is not . Any idea how to solve this?

enter image description here

Uploaded media

enter image description here

my models.py

from django.db import models


class Photo(models.Model):
   image = models.ImageField(upload_to='images/')


class Audio(models.Model):
  audio = models.FileField(upload_to='audios/')


class Video(models.Model):
  video = models.FileField(upload_to='videos/')

my serializers.py

from rest_framework import serializers
from .models import Photo, Audio
from rest_framework import status
from rest_framework.response import Response

class ImageListSerializer(serializers.Serializer):
   image = serializers.ListField(
            child=serializers.FileField(max_length=100, allow_empty_file=False, use_url=True))

   def create(self, validated_data):
      image=validated_data.pop('image')
      for img in image:
        photo=Photo.objects.create(image=img)
        print(photo)
        return photo

   class Meta:
       model = Photo
       fields = ('pk', 'image')


class AudioListSerializer(serializers.Serializer):
  audio = serializers.ListField(
            child=serializers.FileField(max_length=100, allow_empty_file=False, use_url=True))

  def create(self, validated_data):
    audio=validated_data.pop('audio')
    for aud in audio:
        sound=Audio.objects.create(audio=aud,**validated_data)
        print (sound)
    return sound

  class Meta:
    model = Audio
    fields = ('pk', 'audio')

my views.py

from django.shortcuts import render


from rest_framework import viewsets, mixins, status
from rest_framework.settings import api_settings
from .models import Photo, Audio
from .serializers import ImageListSerializer, AudioListSerializer
from rest_framework.parsers import MultiPartParser, FormParser

from rest_framework.response import Response




class PhotoViewSet(viewsets.ModelViewSet):
   serializer_class = ImageListSerializer
   parser_classes = (MultiPartParser, FormParser,)
   queryset = Photo.objects.all()


class AudioViewSet(viewsets.ModelViewSet):
   serializer_class = AudioListSerializer
   parser_classes = (MultiPartParser, FormParser,)
   queryset = Audio.objects.all()
question from:https://stackoverflow.com/questions/65680192/getting-a-huge-null-list-on-response-while-trying-to-upload-through-drf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...