Ahnaf
Ahnaf

Reputation: 159

Can not upload image in django rest frame work

I am trying to upload image in Django. I am successful in saving data of other fields. But images are not uploading.

My model:

from django.db import models
from account.models import Account
# Create your models here.

class Question(models.Model):
  question_id = models.AutoField(primary_key=True)
  user_id = models.ForeignKey(Account,db_column='account_id',on_delete=models.CASCADE)
  type = models.CharField(max_length = 50 ,default= 'General')
  location = models.CharField(max_length =50,default='Dhaka')
  header = models.CharField(max_length =200)
  details = models.CharField(max_length=500)
  date_time = models.DateTimeField(auto_now_add=True)
  vote = models.IntegerField(default=0)
  hidden_flag = models.BooleanField(default=False)
  anonymous = models.IntegerField(default=1)
  image_1 = models.ImageField(blank=True,default='',upload_to='questions/')
  image_2 = models.ImageField(blank=True,default='',upload_to='questions/')

My Serializer:

from rest_framework import serializers
from .models import Question

class QuestionSerializer(serializers.ModelSerializer):
 class Meta:
    model = Question
    fields = ('question_id', 'user_id', 'type', 'location', 'header', 'details', 'date_time','vote','hidden_flag','anonymous','image_1','image_2')
    read_only_fields = ('question_id',)

My views.py:

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
  # Create your views here.

from .serializer import QuestionSerializer
class QuestionView(APIView):
  def post(self,request):
     serealizer = QuestionSerializer(data=request.data)
     if serealizer.is_valid():
         serealizer.save()
         return Response("Created")
     else:
         return Response(serealizer.errors)

At the end of my settings.py file i have included the following code:

MEDIA_ROOT = os.path.join(BASE_DIR,'..','uploaded_media')
MEDIA_URL =  '/media/'

I think I have made some mistakes in my views because when i am trying to upload image with django admin panel. The image upload is successfull. But if i try to upload using postman my image upload is unsuccessful. I see database is populated with default blank value and also the images are not available in the MEDIA_ROOT folder. In the postman i am not getting any errors. Below is screen-shot of my postman request settings.

enter image description here

Below is the Request Code which is generated from postman

POST /questions/ HTTP/1.1
Host: localhost:8000
Cache-Control: no-cache
Postman-Token: 104b89a5-b4c0-4128-c26c-27ac43c59b91
Content-Type: multipart/form-data; boundary=----
WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW 
Content-Disposition: form-data; name="header"

jhkjhkjhkjh
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="details"

this time
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="user_id"

[email protected]
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="image1"; filename=""
Content-Type: 


------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="image2"; filename=""
Content-Type: 


------WebKitFormBoundary7MA4YWxkTrZu0gW--

I am new in django and rest api development. I am confused if i have made the mistake in my views.py or in the request settings of postman.

I have a guess. I think i need to grab the images manually from request variable and save them somehow. But I have no idea if my guess is right and how to do it.

I am using django = 1.10 and Python = 3.6.

Upvotes: 0

Views: 480

Answers (2)

Ykh
Ykh

Reputation: 7717

the name is error in your postman,it's image_1 not image1

Upvotes: 1

Rodrigo Caicedo
Rodrigo Caicedo

Reputation: 46

You need to add your files as another argument in your Serializar:

class QuestionView(APIView):
    def post(self,request):
    serealizer = QuestionSerializer(data=request.data, files=request.FILES.get('file', None))

Files are stored separately than the rest fo the data in the request object. That is why you need to call them apart.

Upvotes: 0

Related Questions