Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

unable to save files to django model through rest api serializers

I am trying to use djangrest api to upload multiple files to a single model, while trying to do so, though I can pass my files through serializer but couldnt save those on my model.

Here is my models.py

from django.db import models

# Create your models here.


class UploadedImage(models.Model):
    img = models.ImageField('Uploaded Image', upload_to='images') # stores uploaded image

 #  dt_created = models.DateTimeField(auto_now_add=True, blank= True, null= True, verbose_name='Created')

my admin.py

from django.contrib import admin

from .models import UploadedImage
# Register your models here.

admin.site.register(UploadedImage)

My viewsets.py

from django.shortcuts import render
from rest_framework import viewsets
from imageUpload.serializers import UploadedImageSerializer    
from imageUpload.models import UploadedImage
from rest_framework.response import Response 
from rest_framework import parsers
from rest_framework import status

# Create your views here.




   class UploadImageViewset(viewsets.ModelViewSet):
   queryset = UploadedImage.objects.all()
   serializer_class = UploadedImageSerializer

   def create(self, request):
          serializer_class = UploadedImageSerializer(data=request.data)
          if 'img' not in request.FILES or not serializer_class.is_valid():
                 return Response(status=status.HTTP_400_BAD_REQUEST)
          else:
                 # Single File
                 # handle_uploaded_file(request.FILES['file'])

                 # Multiple Files
                 files = request.FILES.getlist('img')
                 for f in files:
                        handle_uploaded_file(f)
                 serializer_class.save()
                 return Response(status=status.HTTP_201_CREATED)


 def handle_uploaded_file(f):
   with open(f.name, 'wb+') as destination:
          for chunk in f.chunks():
                 destination.write(chunk)

Here is my serializers.py

from rest_framework import serializers
from .models import UploadedImage

class UploadedImageSerializer(serializers.ModelSerializer):
   
   img = serializers.FileField(max_length=None, allow_empty_file=False)

class Meta:
    model = UploadedImage
    fields = ('pk', 'img')

here is what I get through postman enter image description here

But no images is saved to the mdoel

Another thing, I can't select multiple files through browser calls, any idea why it is happening ? I am new to this and couldn't figure out where I am doing wrong . Any help will be much appreciated


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

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...