Django REST Frameworkを使ってapiで画像を収集します。
#1. Django環境を用意します
- Django Girls Tutorial、Djangoを使ってみる等を参照し、Django環境を用意します
- ここでは例として
restfw
というプロジェクトとします
#2. アプリを用意します
-
python manage.py startapp アプリ名
でアプリを用意します。ここではサンプルとしてupload
というアプリを作成しています - 上記で作成したアプリおよびrest_frameworkを
settings.py
に登録します
restfw/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'upload',
'rest_framework'
]
-
urls.py
に登録します
restfw/urls.py
from django.conf.urls import url, include
from upload.urls import router as upload_router
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^api/', include(upload_router.urls)),]
upload/urls.py
from rest_framework import routers
from .views import ImageViewSet
router = routers.DefaultRouter()
router.register(r'images', ImageViewSet)
-models.py
を記述します
upload/models.py
from django.db import models
class Image(models.Model):
STATUS_DRAFT = "draft"
STATUS_PUBLIC = "published"
STATUS_SET = (
(STATUS_DRAFT, "draft"),
(STATUS_PUBLIC, "published"),
)
filepath = models.CharField(primary_key=True, max_length=1024)
sender = models.CharField(max_length=32)
title = models.CharField(max_length=128)
body = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
lat = models.FloatField(default=34.75)
lng = models.FloatField(default=135.5)
status = models.CharField(choices=STATUS_SET, default=STATUS_DRAFT, max_length=8)
def __str__(self):
return '{}, {}, {}, {}, {}, {}, {}, {}, {}'.format(self.filepath, self.sender, self.title, self.body, self.created_at, self.updated_at, self.lat, self.lng, self.status)
-
serializer.py
を記述します
upload/serializer.py
from rest_framework import serializers
from .models import Image
class ImageSerializer(serializers.ModelSerializer):
class Meta:
model = Image
fields = ('sender', 'title', 'body', 'created_at', 'updated_at', 'lat', 'lng', 'status')
-
views.py
を記述します
upload/views.py
from django.shortcuts import render
import django_filters
from rest_framework import viewsets, filters
from rest_framework.decorators import detail_route, list_route
import os
from rest_framework.response import Response
from .models import Image
from .serializer import ImageSerializer
UPLOAD_DIR = 'static/uploaded_photo/'
class ImageViewSet(viewsets.ModelViewSet):
queryset = Image.objects.all()
serializer_class = ImageSerializer
def create(self, request):
file = request.FILES['file']
path = os.path.join(UPLOAD_DIR, file.name)
destination = open(path, 'wb')
for chunk in file.chunks():
destination.write(chunk)
destination.close()
if not os.path.exists(path):
print('File not found:', path)
return create_render(request)
image, created = Image.objects.get_or_create(filepath=path)
if created:
# image.sender = request.POST['sender']
image.title = request.POST['title']
image.body = request.POST['body']
image.created_at = request.POST['created_at']
image.updated_at = request.POST['updated_at']
image.lat = float(request.POST['lat'])
image.lng = float(request.POST['lng'])
image.status = request.POST['status']
image.save()
return Response({'message': 'OK'})
-static/uploaded_photo
フォルダーを用意します
#3. 試してみます
- Advanced REST Clientを使います
-
Body content type
にmultipart/form-data
を選択して画像を指定してPOSTしてみます
-
static/uploaded_photo
にアップロードされた画像が保存されていることを確認します
-
/api
でAPIマネージャーにアクセスできます