{"id":493,"date":"2017-01-02T20:17:04","date_gmt":"2017-01-02T11:17:04","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2017\/01\/02\/django_rest_framework\/"},"modified":"2017-01-02T20:17:04","modified_gmt":"2017-01-02T11:17:04","slug":"django_rest_framework","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2017\/01\/02\/django_rest_framework\/","title":{"rendered":"Django REST framework \u306e\u52d5\u4f5c\u78ba\u8a8d"},"content":{"rendered":"<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2017\/01\/02\/anacondaspyderdjango_rest\">Anaconda\u3068Spyder\u3067Django REST \u958b\u767a\u74b0\u5883\u3092\u6574\u3048\u308b<\/a> \u3067\u958b\u767a\u74b0\u5883\u3092\u4f5c\u6210\u3057\u305f\u306e\u3067\u3001<a href=\"http:\/\/www.django-rest-framework.org\/\">\u52d5\u4f5c\u78ba\u8a8d<\/a> \u307e\u3067\u884c\u3046\u3002<\/p>\n<p><a href=\"https:\/\/www.amazon.co.jp\/RESTful-Web%E3%82%B5%E3%83%BC%E3%83%93%E3%82%B9-Leonard-Richardson\/dp\/4873113539\/ref=as_li_ss_il?s=books&amp;ie=UTF8&amp;qid=1483355605&amp;sr=1-3&amp;keywords=rest+api&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=17c1874782a8530d044def7db5c4f423\" target=\"_blank\"><img decoding=\"async\" border=\"0\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4873113539&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\"><\/a><img loading=\"lazy\" decoding=\"async\" style=\"border-top-style: none !important; border-left-style: none !important; border-bottom-style: none !important; border-right-style: none !important; margin: 0px\" border=\"0\" alt=\"\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=4873113539\" width=\"1\" height=\"1\">  <\/p>\n<h2>1.\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<h3>1.1 \u5fc5\u8981\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<p>&nbsp;<a href=\"https:\/\/www.typea.info\/blog\/index.php\/2017\/01\/02\/anacondaspyderdjango_rest\">Anaconda\u3068Spyder\u3067Django REST \u958b\u767a\u74b0\u5883\u3092\u6574\u3048\u308b<\/a> \u3092\u53c2\u7167\u3002<\/p>\n<pre class=\"prettyprint linenums\">pip install djangorestframework\r\npip install markdown       # Markdown support for the browsable API.\r\npip install django-filter  # Filtering support<\/pre>\n<h3>1.2 \u8a2d\u5b9a\u306e\u8a18\u8ff0<\/h3>\n<h3>settings.py<\/h3>\n<pre class=\"prettyprint linenums\">INSTALLED_APPS = [\r\n        :\r\n    'rest_framework',\r\n]<\/pre>\n<h3>urls.py<\/h3>\n<p>API\u60c5\u5831\u3092\u95b2\u89a7\u53ef\u80fd\u306b\u3057\u305f\u3044\u5834\u5408\u3001\u30ed\u30b0\u30a4\u30f3\/\u30a2\u30a6\u30c8\u306e\u6a5f\u80fd\u304c\u5fc5\u8981\u306b\u306a\u308b\u3060\u308d\u3046\u3002\u305d\u306e\u5834\u5408\u3001\u30eb\u30fc\u30c8 urls.py \u30d5\u30a1\u30a4\u30eb\u306b\u4ee5\u4e0b\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<pre class=\"prettyprint linenums\">urlpatterns = [\r\n              :\r\n    url(r'^', include(router.urls)),\r\n    url(r'^api-auth\/', include('rest_framework.urls', namespace='rest_framework'))\r\n]\r\n<\/pre>\n<p>URL\u30d1\u30b9\u3092\u6307\u5b9a\u3059\u308b\u3068\u304d\u306b\u306f\u3001&#8217;rest_framework.urls\u2019&nbsp; \u3068\u4e00\u7dd2\u306b\u3001namespace=\u2018rest_framework\u2019&nbsp; \u3092\u542b\u3081\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u304c\u3001Django1.9\u4ee5\u964d\u3067\u306f\u3001REST framework\u304c\u3053\u308c\u3092\u30bb\u30c3\u30c8\u3059\u308b\u305f\u3081\u7701\u7565\u53ef\u80fd\u3002<\/p>\n<h2>2.\u7c21\u5358\u306a\u4f8b<\/h2>\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30e6\u30fc\u30b6\u30fc\u3092\u8aad\u307f\u66f8\u304d\u3067\u304d\u308b\u3001\u7c21\u5358\u306a\u30e2\u30c7\u30eb\u30d9\u30fc\u30b9\u306eAPI\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n<h3>2.1 \u8a2d\u5b9a<\/h3>\n<p>REST framework API \u306e\u305f\u3081\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u8a2d\u5b9a\u306f\u3059\u3079\u3066\u3001REST_FRAMEWORK \u306b\u4fdd\u6301\u3055\u308c\u308b\u3002settings.py \u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u4ee5\u4e0b\u3092\u8ffd\u8a18\u3059\u308b\u3002<\/p>\n<h3>settings.py<\/h3>\n<pre class=\"prettyprint linenums\">REST_FRAMEWORK = {\r\n    # Use Django's standard `django.contrib.auth` permissions,\r\n    # or allow read-only access for unauthenticated users.\r\n    'DEFAULT_PERMISSION_CLASSES': [\r\n        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'\r\n    ]\r\n}<\/pre>\n<h3>2.2 Django\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u6e96\u5099<\/h3>\n<p><a title=\"http:\/\/djangoproject.jp\/doc\/ja\/1.0\/ref\/django-admin.html\" href=\"http:\/\/djangoproject.jp\/doc\/ja\/1.0\/ref\/django-admin.html\">http:\/\/djangoproject.jp\/doc\/ja\/1.0\/ref\/django-admin.html<\/a><\/p>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u4f5c\u6210\u3057\u3001\u30b9\u30fc\u30d1\u30fc\u30e6\u30fc\u30b6\u30fc\u3092\u767b\u9332\u3059\u308b\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2017\/01\/02\/anacondaspyderdjango_rest\">Anaconda\u3068Spyder\u3067Django REST \u958b\u767a\u74b0\u5883\u3092\u6574\u3048\u308b<\/a> \u3067\u3057\u305f\u3088\u3046\u306b\u3001Anaconda\u306e\u74b0\u5883\u304b\u3089\u3001\u30bf\u30fc\u30df\u30ca\u30eb\u3092\u8d77\u52d5\u3059\u308b\u3002<\/p>\n<h3>manage.py \u306e HELP \u3092 \u78ba\u8a8d python manage.py \u2013help<\/h3>\n<pre class=\"prettyprint linenums\">(C:\\Programs\\Anaconda3\\envs\\webapp) C:\\Users\\piroto\\Documents\\Spyder\\WebApp\\restsample&gt;python manage.py --help\r\n\r\nType 'manage.py help <subcommand>' for help on a specific subcommand.\r\n\r\nAvailable subcommands:\r\n\r\n[auth]\r\n    changepassword\r\n    createsuperuser\r\n\r\n[django]\r\n    check\r\n    compilemessages\r\n    createcachetable\r\n    dbshell\r\n    diffsettings\r\n    dumpdata\r\n    flush\r\n    inspectdb\r\n    loaddata\r\n    makemessages\r\n    makemigrations\r\n    migrate\r\n    sendtestemail\r\n    shell\r\n    showmigrations\r\n    sqlflush\r\n    sqlmigrate\r\n    sqlsequencereset\r\n    squashmigrations\r\n    startapp\r\n    startproject\r\n    test\r\n    testserver\r\n\r\n[sessions]\r\n    clearsessions\r\n\r\n[staticfiles]\r\n    collectstatic\r\n    findstatic\r\n    runserver<\/pre>\n<h3>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u4f5c\u6210<\/h3>\n<p>settings.py\u306e\u8a18\u8ff0\u306b\u5f93\u3044\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u4f5c\u6210\u3059\u308b\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u307e\u307esqlite \u3092\u5229\u7528\u3059\u308b<\/p>\n<p>python manage.py migrate<\/p>\n<p><a title=\"https:\/\/docs.djangoproject.com\/en\/1.10\/intro\/tutorial02\/\" href=\"https:\/\/docs.djangoproject.com\/en\/1.10\/intro\/tutorial02\/\">https:\/\/docs.djangoproject.com\/en\/1.10\/intro\/tutorial02\/<\/a><\/p>\n<pre class=\"prettyprint linenums\">(C:\\Programs\\Anaconda3\\envs\\webapp) C:\\Users\\piroto\\Documents\\Spyder\\WebApp\\restsample&gt;python manage.py migrate\r\nOperations to perform:\r\n  Apply all migrations: admin, auth, contenttypes, sessions\r\nRunning migrations:\r\n  Applying contenttypes.0001_initial... OK\r\n  Applying auth.0001_initial... OK\r\n  Applying admin.0001_initial... OK\r\n  Applying admin.0002_logentry_remove_auto_add... OK\r\n  Applying contenttypes.0002_remove_content_type_name... OK\r\n  Applying auth.0002_alter_permission_name_max_length... OK\r\n  Applying auth.0003_alter_user_email_max_length... OK\r\n  Applying auth.0004_alter_user_username_opts... OK\r\n  Applying auth.0005_alter_user_last_login_null... OK\r\n  Applying auth.0006_require_contenttypes_0002... OK\r\n  Applying auth.0007_alter_validators_add_error_messages... OK\r\n  Applying auth.0008_alter_user_username_max_length... OK\r\n  Applying sessions.0001_initial... OK<\/pre>\n<h3>admin\u30e6\u30fc\u30b6\u30fc\u3092\u4f5c\u6210<\/h3>\n<p>python manage.py createsuperuser<\/p>\n<pre class=\"prettyprint linenums\">(C:\\Programs\\Anaconda3\\envs\\webapp) C:\\Users\\piroto\\Documents\\Spyder\\WebApp\\restsample&gt;python manage.py createsuperuser\r\nUsername (leave blank to use 'piroto'):\r\nEmail address: pppiroto@gmail.com\r\nPassword:\r\nPassword (again):\r\nSuperuser created successfully.<\/pre>\n<h3>2.2 \u52d5\u4f5c\u78ba\u8a8d<\/h3>\n<p>\u3053\u308c\u3067\u6e96\u5099\u304c\u6574\u3063\u305f\u306e\u3067\u3001<a href=\"http:\/\/localhost:8000\">http:\/\/localhost:8000<\/a> \u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3002<\/p>\n<p>API \u306e\u8aac\u660e\u304c\u8868\u793a\u3055\u308c\u308b<\/p>\n<p><a href=\"http:\/\/typea.info\/blg\/glob\/Windows-Live-Writer\/Django-REST-framework_EA92\/django_rest_api01_2.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"django_rest_api01\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"django_rest_api01\" src=\"http:\/\/typea.info\/blg\/glob\/Windows-Live-Writer\/Django-REST-framework_EA92\/django_rest_api01_thumb.jpg\" width=\"348\" height=\"271\"><\/a><\/p>\n<p><a href=\"http:\/\/localhost:8000\/users\">http:\/\/localhost:8000\/users<\/a> \u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u304c\u53d6\u5f97\u3067\u304d\u308b\u3002<\/p>\n<p><a href=\"http:\/\/typea.info\/blg\/glob\/Windows-Live-Writer\/Django-REST-framework_EA92\/django_rest_api02_2.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"django_rest_api02\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"django_rest_api02\" src=\"http:\/\/typea.info\/blg\/glob\/Windows-Live-Writer\/Django-REST-framework_EA92\/django_rest_api02_thumb.jpg\" width=\"348\" height=\"248\"><\/a><\/p>\n<p>\u753b\u9762\u306e\u4e0b\u90e8\u306b\u306f\u3001\u30d5\u30a9\u30fc\u30e0\u304b\u3089\u30e6\u30fc\u30b6\u30fc\u767b\u9332\u3092\u884c\u3046UI\u304c\u5b58\u5728\u3059\u308b\u3002<\/p>\n<p>\u305f\u3060\u3001\u3053\u306eUI\u304b\u3089\u306f\u3001REST API \u3092\u4f7f\u7528\u3057\u3066\u767b\u9332\u3057\u3066\u3044\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u3088\u3046\u3060\u3002<\/p>\n<p><a href=\"http:\/\/typea.info\/blg\/glob\/Windows-Live-Writer\/Django-REST-framework_EA92\/django_rest_api03_2.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"django_rest_api03\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"django_rest_api03\" src=\"http:\/\/typea.info\/blg\/glob\/Windows-Live-Writer\/Django-REST-framework_EA92\/django_rest_api03_thumb.jpg\" width=\"349\" height=\"258\"><\/a><\/p>\n<p><a href=\"http:\/\/localhost:8080\/users?format=json\">http:\/\/localhost:8080\/users?format=json<\/a><\/p>\n<p>\u3067JSON\u304c\u8fd4\u308b\u3053\u3068\u3092\u78ba\u8a8d<\/p>\n<p><a href=\"http:\/\/typea.info\/blg\/glob\/Windows-Live-Writer\/Django-REST-framework_EA92\/django_rest_api04_2.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"django_rest_api04\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"django_rest_api04\" src=\"http:\/\/typea.info\/blg\/glob\/Windows-Live-Writer\/Django-REST-framework_EA92\/django_rest_api04_thumb.jpg\" width=\"351\" height=\"163\"><\/a><\/p>\n<p><a href=\"https:\/\/www.amazon.co.jp\/Web-API-Parts-%E6%B0%B4%E9%87%8E-%E8%B2%B4%E6%98%8E\/dp\/4873116864\/ref=as_li_ss_il?s=books&amp;ie=UTF8&amp;qid=1483355605&amp;sr=1-1&amp;keywords=rest+api&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=900c1370882562aa827f669b35538fcc\" target=\"_blank\"><img decoding=\"async\" border=\"0\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4873116864&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\"><\/a><img loading=\"lazy\" decoding=\"async\" style=\"border-top-style: none !important; border-left-style: none !important; border-bottom-style: none !important; border-right-style: none !important; margin: 0px\" border=\"0\" alt=\"\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=4873116864\" width=\"1\" height=\"1\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Anaconda\u3068Spyder\u3067Django REST \u958b\u767a\u74b0\u5883\u3092\u6574\u3048\u308b \u3067\u958b\u767a\u74b0\u5883\u3092\u4f5c\u6210\u3057\u305f\u306e\u3067\u3001\u52d5\u4f5c\u78ba\u8a8d \u307e\u3067\u884c\u3046\u3002 1.\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb 1.1 \u5fc5\u8981\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb &nbsp;Anaconda\u3068Spyder [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[95,29,20],"tags":[],"class_list":["post-493","post","type-post","status-publish","format-standard","hentry","category-anaconda","category-django","category-python"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=493"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/493\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}