Django REST FileUpload serializer returns {'file': None}
<p>Iâve been working on a Django project that requires a file upload. I use API approach in my app designs using django-rest-framework. I created my model, APIView, and serializer but unfortunately every time the request goes through the serializer the upload.data returns {'file': None}. If I just use request.FILES['file'] it returns the file no problem but I want to use the serialized data. I'm using dropzone js on the front end to upload the file. Here is my code below. </p>

<p>HTML</p>

<pre><code>{% extends 'base_profile.html' %}
{% load static %}
{% block title %}File Import{% endblock %}
{% block pagetitle %}File Import{% endblock %}
{% block content %}
&lt;div class="widget"&gt;
    &lt;div class="widget-heading clearfix"&gt;
      &lt;h3 class="widget-title pull-left list-inline"&gt;CSV &lt;/h3&gt;
      &lt;button type="button" class="btn btn-primary pull-right"&gt;&lt;i class="ti-upload mr-5"&gt;&lt;/i&gt; Upload&lt;/button&gt;
    &lt;/div&gt;
    &lt;div class="widget-body"&gt;
      &lt;form id="type-dz" class="dropzone"&gt;{% csrf_token %}&lt;/form&gt;
    &lt;/div&gt;
&lt;/div&gt;

{% endblock %}
{% block js %}
&lt;script type="text/javascript"&gt;
    $("#type-dz").dropzone({
        url: "{% url 'api_import:file' %}",
        paramName: "file",
        acceptedFiles: ".csv",
        maxFilesize: 2,
        maxThumbnailFilesize: .5,
        dictDefaultMessage: "&lt;i class='icon-dz fa fa-file-text-o'&gt;&lt;/i&gt;Drop files here to upload"
});
&lt;/script&gt;
{% endblock %}
</code></pre>

<p>urls.py</p>

<pre><code>urlpatterns = [
    url(r'^api/import/', include('api.import_api.urls', namespace="api_import")),
]
</code></pre>

<p>api/urls.py</p>

<pre><code>urlpatterns = [
    url(r'^file/', FileImport.as_view(), name='file'),
]
</code></pre>

<p>views.py</p>

<pre><code>class FileImport(APIView):
    parser_classes = (MultiPartParser, FormParser,)
    serializer = ImportSerializer

    def post(self, request, format=None):
        upload = self.serializer(data=request.FILES)

        if upload.is_valid():
            file = FileUpload(file=upload.data['file'], uploaded_by=request.user.profile)
            file.save()
            return Response({'success': 'Imported successfully'})
        else:
            return Response(upload.errors, status=400)
</code></pre>

<p>serializers.py</p>

<pre><code>class ImportSerializer(serializers.Serializer):
    file = serializers.FileField()
</code></pre>

<p>models.py</p>

<pre><code>class FileUpload(models.Model):
    file = models.FileField(upload_to='files/%Y/%m/%d')
    date_uploaded = models.DateTimeField(auto_now=True)
    uploaded_by = models.ForeignKey('UserProfile', blank=True, null=True)
</code></pre>

<p>It would be helpful to see how you are uploading the file. If you are using a multipart/form-data request and providing the json for "file" properly, the most likely thing is that the file is failing validation for some reason.</p>

<p>If you can, it might also be helpful to test from the browsable api (as that guarantees nothing is wrong with your request).</p>

<p>Edit:<br>
The issue was that the <code>validated_data</code> field should be used instead of the  <code>data</code> field after calling <code>is_valid()</code>.</p>

python django django-rest-framework django-serializer