├── .envrc ├── .gitignore ├── LICENSE ├── README.md ├── requirements.txt └── src └── django_rest_framework_test ├── blog ├── __init__.py ├── admin.py ├── apps.py ├── migrations │ ├── 0001_initial.py │ ├── __init__.py │ └── __pycache__ │ │ ├── 0001_initial.cpython-35.pyc │ │ └── __init__.cpython-35.pyc ├── models.py ├── serializer.py ├── tests.py ├── urls.py └── views.py ├── django_rest_framework_test ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py └── manage.py /.envrc: -------------------------------------------------------------------------------- 1 | layout python3 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | 59 | db.sqlite3 60 | .direnv/ 61 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 kimihiro_n 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django REST Frameworkを試してみた 2 | 3 | [Django REST Frameworkを使って爆速でAPIを実装する](http://qiita.com/kimihiro_n/items/86e0a9e619720e57ecd8) 4 | * http://qiita.com/kimihiro_n/items/86e0a9e619720e57ecd8 5 | 6 | 7 | * Djangoのセットアップ 8 | * `Blog` Appの作成 9 | * Django REST APIの組み込み 10 | 11 | ## Setup 12 | 13 | ```bash 14 | mkvirtualenv django_rest_framework_test --python=`which python3` 15 | pip install -r requirements.txt 16 | cd src/django_rest_fraamework_test 17 | python manage.py migrate 18 | python manage.py createsuperuser 19 | python manage.py runserver 20 | ``` 21 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | django 2 | djangorestframework 3 | django-filter 4 | 5 | -------------------------------------------------------------------------------- /src/django_rest_framework_test/blog/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pistatium/django_rest_framework_test/60a0f5183a0400e2f1096b18bd43c8703556fd65/src/django_rest_framework_test/blog/__init__.py -------------------------------------------------------------------------------- /src/django_rest_framework_test/blog/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | from .models import User, Entry 4 | 5 | 6 | @admin.register(User) 7 | class UserAdmin(admin.ModelAdmin): 8 | pass 9 | 10 | @admin.register(Entry) 11 | class Entry(admin.ModelAdmin): 12 | pass 13 | -------------------------------------------------------------------------------- /src/django_rest_framework_test/blog/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class BlogConfig(AppConfig): 5 | name = 'blog' 6 | -------------------------------------------------------------------------------- /src/django_rest_framework_test/blog/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by Django 1.9 on 2015-12-08 05:15 3 | from __future__ import unicode_literals 4 | 5 | from django.db import migrations, models 6 | import django.db.models.deletion 7 | 8 | 9 | class Migration(migrations.Migration): 10 | 11 | initial = True 12 | 13 | dependencies = [ 14 | ] 15 | 16 | operations = [ 17 | migrations.CreateModel( 18 | name='Entry', 19 | fields=[ 20 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 21 | ('title', models.CharField(max_length=128)), 22 | ('body', models.TextField()), 23 | ('created_at', models.DateTimeField(auto_now_add=True)), 24 | ('updated_at', models.DateTimeField(auto_now=True)), 25 | ('status', models.CharField(choices=[('draft', '下書き'), ('public', '公開中')], default='draft', max_length=8)), 26 | ], 27 | ), 28 | migrations.CreateModel( 29 | name='User', 30 | fields=[ 31 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 32 | ('name', models.CharField(max_length=32)), 33 | ('mail', models.EmailField(max_length=254)), 34 | ], 35 | ), 36 | migrations.AddField( 37 | model_name='entry', 38 | name='author', 39 | field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='entries', to='blog.User'), 40 | ), 41 | ] 42 | -------------------------------------------------------------------------------- /src/django_rest_framework_test/blog/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pistatium/django_rest_framework_test/60a0f5183a0400e2f1096b18bd43c8703556fd65/src/django_rest_framework_test/blog/migrations/__init__.py -------------------------------------------------------------------------------- /src/django_rest_framework_test/blog/migrations/__pycache__/0001_initial.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pistatium/django_rest_framework_test/60a0f5183a0400e2f1096b18bd43c8703556fd65/src/django_rest_framework_test/blog/migrations/__pycache__/0001_initial.cpython-35.pyc -------------------------------------------------------------------------------- /src/django_rest_framework_test/blog/migrations/__pycache__/__init__.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pistatium/django_rest_framework_test/60a0f5183a0400e2f1096b18bd43c8703556fd65/src/django_rest_framework_test/blog/migrations/__pycache__/__init__.cpython-35.pyc -------------------------------------------------------------------------------- /src/django_rest_framework_test/blog/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class User(models.Model): 5 | name = models.CharField(max_length=32) 6 | mail = models.EmailField() 7 | 8 | def __repr__(self): 9 | return "{}: {}".format(self.pk, self.name) 10 | 11 | __str__ = __repr__ 12 | 13 | 14 | class Entry(models.Model): 15 | STATUS_DRAFT = "draft" 16 | STATUS_PUBLIC = "public" 17 | STATUS_SET = ( 18 | (STATUS_DRAFT, "下書き"), 19 | (STATUS_PUBLIC, "公開中"), 20 | ) 21 | title = models.CharField(max_length=128) 22 | body = models.TextField() 23 | created_at = models.DateTimeField(auto_now_add=True) 24 | updated_at = models.DateTimeField(auto_now=True) 25 | status = models.CharField(choices=STATUS_SET, default=STATUS_DRAFT, max_length=8) 26 | author = models.ForeignKey(User, related_name='entries') 27 | 28 | def __repr__(self): 29 | return "{}: {}".format(self.pk, self.title) 30 | 31 | __str__ = __repr__ 32 | -------------------------------------------------------------------------------- /src/django_rest_framework_test/blog/serializer.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | from rest_framework import serializers 4 | 5 | from .models import User, Entry 6 | 7 | 8 | class UserSerializer(serializers.ModelSerializer): 9 | class Meta: 10 | model = User 11 | fields = ('id', 'name', 'mail') 12 | 13 | 14 | class EntrySerializer(serializers.ModelSerializer): 15 | author = UserSerializer(read_only=True) 16 | 17 | class Meta: 18 | model = Entry 19 | fields = ('id', 'title', 'body', 'created_at', 'status', 'author') 20 | 21 | -------------------------------------------------------------------------------- /src/django_rest_framework_test/blog/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /src/django_rest_framework_test/blog/urls.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | from rest_framework import routers 4 | from .views import UserViewSet, EntryViewSet 5 | 6 | 7 | router = routers.DefaultRouter() 8 | router.register(r'users', UserViewSet) 9 | router.register(r'entries', EntryViewSet) 10 | 11 | -------------------------------------------------------------------------------- /src/django_rest_framework_test/blog/views.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | import django_filters 4 | from rest_framework import viewsets, filters 5 | 6 | from .models import User, Entry 7 | from .serializer import UserSerializer, EntrySerializer 8 | 9 | 10 | class UserViewSet(viewsets.ModelViewSet): 11 | queryset = User.objects.all() 12 | serializer_class = UserSerializer 13 | 14 | 15 | class EntryViewSet(viewsets.ModelViewSet): 16 | queryset = Entry.objects.all() 17 | serializer_class = EntrySerializer 18 | filter_fields = ('author', 'status') 19 | 20 | -------------------------------------------------------------------------------- /src/django_rest_framework_test/django_rest_framework_test/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pistatium/django_rest_framework_test/60a0f5183a0400e2f1096b18bd43c8703556fd65/src/django_rest_framework_test/django_rest_framework_test/__init__.py -------------------------------------------------------------------------------- /src/django_rest_framework_test/django_rest_framework_test/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for django_rest_framework_test project. 3 | 4 | Generated by 'django-admin startproject' using Django 1.9. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.9/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/1.9/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'prw7@pr*tdm#a5a*hgudss-sp=q)whtu!qums5ui($(29ws_==' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'blog', 41 | 'rest_framework', 42 | ] 43 | 44 | MIDDLEWARE_CLASSES = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 51 | 'django.contrib.messages.middleware.MessageMiddleware', 52 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 53 | ] 54 | 55 | ROOT_URLCONF = 'django_rest_framework_test.urls' 56 | 57 | TEMPLATES = [ 58 | { 59 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 60 | 'DIRS': [], 61 | 'APP_DIRS': True, 62 | 'OPTIONS': { 63 | 'context_processors': [ 64 | 'django.template.context_processors.debug', 65 | 'django.template.context_processors.request', 66 | 'django.contrib.auth.context_processors.auth', 67 | 'django.contrib.messages.context_processors.messages', 68 | ], 69 | }, 70 | }, 71 | ] 72 | 73 | WSGI_APPLICATION = 'django_rest_framework_test.wsgi.application' 74 | 75 | 76 | # Database 77 | # https://docs.djangoproject.com/en/1.9/ref/settings/#databases 78 | 79 | DATABASES = { 80 | 'default': { 81 | 'ENGINE': 'django.db.backends.sqlite3', 82 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 83 | } 84 | } 85 | 86 | 87 | # Password validation 88 | # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators 89 | 90 | AUTH_PASSWORD_VALIDATORS = [ 91 | { 92 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 93 | }, 94 | { 95 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 96 | }, 97 | { 98 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 99 | }, 100 | { 101 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 102 | }, 103 | ] 104 | 105 | 106 | # Internationalization 107 | # https://docs.djangoproject.com/en/1.9/topics/i18n/ 108 | 109 | LANGUAGE_CODE = 'en-us' 110 | 111 | TIME_ZONE = 'UTC' 112 | 113 | USE_I18N = True 114 | 115 | USE_L10N = True 116 | 117 | USE_TZ = True 118 | 119 | 120 | # Static files (CSS, JavaScript, Images) 121 | # https://docs.djangoproject.com/en/1.9/howto/static-files/ 122 | 123 | STATIC_URL = '/static/' 124 | 125 | REST_FRAMEWORK = { 126 | 'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',), 127 | 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', 128 | 'PAGE_SIZE': 2 129 | } 130 | -------------------------------------------------------------------------------- /src/django_rest_framework_test/django_rest_framework_test/urls.py: -------------------------------------------------------------------------------- 1 | """django_rest_framework_test URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/1.9/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Add an import: from blog import urls as blog_urls 14 | 2. Import the include() function: from django.conf.urls import url, include 15 | 3. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls)) 16 | """ 17 | from django.conf.urls import url, include 18 | from django.contrib import admin 19 | 20 | from blog.urls import router as blog_router 21 | 22 | urlpatterns = [ 23 | url(r'^admin/', admin.site.urls), 24 | url(r'^api/', include(blog_router.urls)), 25 | ] 26 | -------------------------------------------------------------------------------- /src/django_rest_framework_test/django_rest_framework_test/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for django_rest_framework_test project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_rest_framework_test.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /src/django_rest_framework_test/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_rest_framework_test.settings") 7 | 8 | from django.core.management import execute_from_command_line 9 | 10 | execute_from_command_line(sys.argv) 11 | --------------------------------------------------------------------------------