4

While trying to upload the file using Postman with Content-Type - multipart/form-data in the headers. I am passing both the fields, but I get the following error:

Error:{"upload_to": ["This field is required."],"file_object": ["No file was submitted."]}

urls.py

from django.conf.urls import include, url
from rest_framework_nested import routers
from utils.views import TemporaryImageView
from . import  views

router = routers.SimpleRouter(trailing_slash=False)

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^upload-temp-image/$', TemporaryImageView.as_view())
]

views.py

from rest_framework import viewsets, filters
import django_filters.rest_framework
from rest_framework.generics import CreateAPIView
from rest_framework.parsers import FileUploadParser, MultiPartParser, FormParser
from utils.serializers.temporary_image import TemporaryImageSerializer

class TemporaryImageView(CreateAPIView):
    parser_classes = (MultiPartParser,)
    serializer_class = TemporaryImageSerializer

serializers.py

from rest_framework import serializers
from utils.models.tempfile import TemporaryFile

class TemporaryImageSerializer(serializers.ModelSerializer):
    choices = (('Company Logo','/company/logos/'),
               )
    upload_to = serializers.ChoiceField(choices=choices)
    file_object = serializers.ImageField()

    class Meta:
        model = TemporaryFile
        fields = ('upload_to', 'file_object')

models.py

from django.db import models
class TemporaryFile(models.Model):
    """
    a temporary file to backend
    """

    file_object = models.FileField(blank=False, null=False)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return str(self.id)

Please help...I don't know what is wrong.

2
  • Did you remember to include enctype="multipart/form-data" in your template where the form is submitted? Commented Nov 20, 2018 at 7:15
  • yes..i am providing Commented Nov 20, 2018 at 8:29

1 Answer 1

2

I have changed my models.py file in the way below and it works as expected..

models.py

from django.db import models

def get_image_path(instance, filename):

    if instance.upload_to == "company_logo":
        path = 'company/logo/'
    return path


class TemporaryFile(models.Model):
    """
    a temporary file to backend
    """

    file_object = models.FileField(blank=False, null=False, upload_to=get_image_path)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return str(self.id)

    def __init__(self, *args, **kwargs):
        self.upload_to = kwargs.pop("upload_to")
        super(TemporaryFile, self).__init__(*args, **kwargs)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.