Timo Tomate
Timo Tomate

Reputation: 13

How to send 'image' via POSTMAN? (Django Image Field)

I'd like to send image via postman like this and save to 'media' directory but don't know how to do this.

enter image description here

And these are my codes that I use.

  1. models.py

    class Article(models.Model):
    
         emotion = models.TextField()
         location = models.TextField()
         menu = models.TextField()
         weather = models.TextField()
         song = models.TextField()
         point = models.IntegerField()
         content = models.TextField()
    
         image = models.ImageField(default = 'media/coffee_default.jpg', upload_to = "%Y/%m/%d")
         user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
         created = models.DateTimeField(default = now)
         liked = models.BooleanField(default = False)
         num_liked = models.IntegerField(default = 0)
    
  2. views.py

@api_view(['POST']) @permission_classes([]) def post_articles(request): user = request.user body = json.loads(request.body)

article = Article(
    emotion = body['emotion'],
    location = body['location'],
    menu = body['menu'],
    weather = body['weather'],
    image = body['image'],
    song = body['song'],
    point = body['point'],
    content = body['content'],
    user = user)
article.save()

serializers = ArticleSerializer(article)

return JsonResponse({ "success": True, "data": serializers.data })
  1. serializers.py

    class ArticleSerializer(serializers.ModelSerializer): class Meta: model = Article fields = ['emotion', 'location', 'menu', 'weather', 'song', 'point', 'content', 'image', 'user', 'created', 'liked', 'num_liked']

Upvotes: 1

Views: 1015

Answers (1)

cedric
cedric

Reputation: 367

From my experience, you don't need to do anything particular about file fields, you just tell it to make use of the file field:

from rest_framework import routers, serializers, viewsets

class Article(django.db.models.Model):
    image = django.db.models.ImageField()
# rest of fields are excluded --TODO

class ArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Article
        fields = ('id', 
                 'image', # <-- HERE
                 'menu', 'weather', 'song', 'point', 'content', 'location', 'emotion')   

class ArticleViewSet(viewsets.ModelViewSet):
    queryset = models.Article.objects.all()
    serializer_class = ArticleSerializer

router = routers.DefaultRouter()
router.register(r'photos', PhotoViewSet)

api_urlpatterns = ([
    url('', include(router.urls)),
], 'api')
urlpatterns += [
    url(r'^api/', include(api_urlpatterns)),
]

and you're ready to upload files:

curl -sS http://example.com/api/photos/ -F 'image=@/path/to/file' Add -F field=value for each extra field your model has. And don't forget to add authentication.

or with dothttp

POST http://example.com/api/articles/ 
multipart(
    'image'< '<file to the path>',
    'emotion' < "emotion",
    'location' < "location",
    'menu' < "menu",
    'weather' < "weather",
    'image' < "image",
    'song' < "song",
    'point' < "point",
    'content' < "content"
)

Upvotes: 1

Related Questions