Wagtail API v2 Configuration Guide — Wagtail 2.0a0 documentation を
試してみた
[TOC]
前提
以下の
OS
% sw_vers ProductName: Mac OS X ProductVersion: 10.13.1 BuildVersion: 17B1003
Python の
Version % python3 -V Python 3.6.2
wagtail の
Version % python3 -m pip list --format=columns | grep wagtail wagtail 1.13.1
wagtail プロジェクトに
ついて wagtail start mysite
mysite
という プロジェクトを 作っている 想定で、 パスは 記載します。 取得する
データに ついて
wagtail Your first Wagtail site を参考に、 Blog を 作る | Monotalk で Blog を 作成しています。
このデータを 取得する API を 記載します。
設定
INSTALLED_APPS に wagtail.api.v2
を 追加する
wagtail.api.v2
をbase.py
に
- mysite/mysite/settings/base.py
INSTALLED_APPS = [ ... 'wagtail.api.v2', ... }
api.py を 作成する
api.py
を
Page データ取得用途、
- mysite/mysite/api.py1
# api.py from wagtail.api.v2.endpoints import PagesAPIEndpoint from wagtail.api.v2.router import WagtailAPIRouter from wagtail.wagtailimages.api.v2.endpoints import ImagesAPIEndpoint from wagtail.wagtaildocs.api.v2.endpoints import DocumentsAPIEndpoint # Create the router. "wagtailapi" is the URL namespace api_router = WagtailAPIRouter('wagtailapi') # Add the three endpoints using the "register_endpoint" method. # The first parameter is the name of the endpoint (eg. pages, images). This # is used in the URL of the endpoint # The second parameter is the endpoint class that handles the requests api_router.register_endpoint('pages', PagesAPIEndpoint) api_router.register_endpoint('images', ImagesAPIEndpoint) api_router.register_endpoint('documents', DocumentsAPIEndpoint)
urls.py に、 api の url を 登録する
api の
from .api import api_router urlpatterns = [ ... url(r'^api/v2/', api_router.urls), ... ]
API 実行
Page API
http://127.0.0.1:8000/api/v2/pages
にアクセスすると、 以下のような JSONが 返却されます。 { "meta": { "total_count": 6 }, "items": [ { "id": 3, "meta": { "type": "home.HomePage", "detail_url": "http://localhost/api/v2/pages/3/", "html_url": "http://localhost/", "slug": "home", "first_published_at": null }, "title": "Home" }, { "id": 4, "meta": { "type": "blog.BlogIndexPage", "detail_url": "http://localhost/api/v2/pages/4/", "html_url": "http://localhost/blog/", "slug": "blog", "first_published_at": "2018-01-13T15:25:27.312208Z" }, "title": "Blog Index Page" }, { "id": 5, "meta": { "type": "blog.BlogPage", "detail_url": "http://localhost/api/v2/pages/5/", "html_url": "http://localhost/blog/my-first-post/", "slug": "my-first-post", "first_published_at": "2018-01-13T15:30:54.114605Z" }, "title": "My first Post" }, { "id": 6, "meta": { "type": "blog.BlogPage", "detail_url": "http://localhost/api/v2/pages/6/", "html_url": "http://localhost/blog/my-first-post/my-seconds-post/", "slug": "my-seconds-post", "first_published_at": "2018-01-13T15:29:14.568634Z" }, "title": "My seconds Post" }, { "id": 7, "meta": { "type": "blog.BlogPage", "detail_url": "http://localhost/api/v2/pages/7/", "html_url": "http://localhost/blog/my-se/", "slug": "my-se", "first_published_at": "2018-01-13T15:31:55.598228Z" }, "title": "My seconds Post" }, { "id": 8, "meta": { "type": "blog.BlogPage", "detail_url": "http://localhost/api/v2/pages/8/", "html_url": "http://localhost/blog/my-third-post/", "slug": "my-third-post", "first_published_at": "2018-01-13T15:32:48.907954Z" }, "title": "My third Post" } ] }
Image API
http://127.0.0.1:8000/api/v2/images
にアクセスすると、 以下 JSON が 返却されます。 登録が ないので、 空です。 { "meta": { "total_count": 0 }, "items": [] }
Document API
http://127.0.0.1:8000/api/v2/documents
にアクセスすると、 以下 JSON が 返却されます。 登録が ないので、 空です。 { "meta": { "total_count": 0 }, "items": [] }
Page API を 条件を 指定して 実行する
Blog データだけを
Blog 記事のみを
指定して 取得する http://127.0.0.1:8000/api/v2/pages?type=blog.BlogPage
でアクセスすると、 以下 JSON が 返却されます。
BlogPage のみに絞り込まれます。 { "meta": { "total_count": 4 }, "items": [ { "id": 5, "meta": { "type": "blog.BlogPage", "detail_url": "http://localhost/api/v2/pages/5/", "html_url": "http://localhost/blog/my-first-post/", "slug": "my-first-post", "first_published_at": "2018-01-13T15:30:54.114605Z" }, "title": "My first Post" }, { "id": 6, "meta": { "type": "blog.BlogPage", "detail_url": "http://localhost/api/v2/pages/6/", "html_url": "http://localhost/blog/my-first-post/my-seconds-post/", "slug": "my-seconds-post", "first_published_at": "2018-01-13T15:29:14.568634Z" }, "title": "My seconds Post" }, { "id": 7, "meta": { "type": "blog.BlogPage", "detail_url": "http://localhost/api/v2/pages/7/", "html_url": "http://localhost/blog/my-se/", "slug": "my-se", "first_published_at": "2018-01-13T15:31:55.598228Z" }, "title": "My seconds Post" }, { "id": 8, "meta": { "type": "blog.BlogPage", "detail_url": "http://localhost/api/v2/pages/8/", "html_url": "http://localhost/blog/my-third-post/", "slug": "my-third-post", "first_published_at": "2018-01-13T15:32:48.907954Z" }, "title": "My third Post" } ] }
Blog 記事のみを
ページング指定して 取得する http://127.0.0.1:8000/api/v2/pages?type=blog.BlogPage&offset=3&limit=1
でアクセスすると、 以下 JSON が 返却されます。
4件目のみ、1件だけ 取得できます。 { "meta": { "total_count": 4 }, "items": [ { "id": 8, "meta": { "type": "blog.BlogPage", "detail_url": "http://localhost/api/v2/pages/8/", "html_url": "http://localhost/blog/my-third-post/", "slug": "my-third-post", "first_published_at": "2018-01-13T15:32:48.907954Z" }, "title": "My third Post" } ] }
取得項目を 拡張する
BlogPage
に
mysite/blog/models.py
from wagtail.api import APIField class BlogPage(Page): date = models.DateField("Post date") intro = models.CharField(max_length=250) body = RichTextField(blank=True) search_fields = Page.search_fields + [ index.SearchField('intro'), index.SearchField('body'), ] content_panels = Page.content_panels + [ FieldPanel('date'), FieldPanel('intro'), FieldPanel('body', classname="full"), ] api_fields = [ APIField('date'), APIField('intro'), APIField('body'), ]
Blog 記事のみを
ページング指定して 取得する http://127.0.0.1:8000/api/v2/pages?type=blog.BlogPage&offset=3&limit=1&fields=date,intro,body
にアクセスすると、 以下のような JSON が 返却されます。
field に取得項目と して 指定しないと、 date
、intro
、body
は取得できませんでした。 { "meta": { "total_count": 4 }, "items": [ { "id": 8, "meta": { "type": "blog.BlogPage", "detail_url": "http://localhost/api/v2/pages/8/", "html_url": "http://localhost/blog/my-third-post/", "slug": "my-third-post", "first_published_at": "2018-01-13T15:32:48.907954Z" }, "title": "My third Post", "date": "2018-01-14", "intro": "My third Blog Post", "body": "<p>My third Blog Post</p>" } ] }
API で
wagtail/endpoints.py at master · wagtail/wagtail を
そちらは、
以上です。
開発中の
version 2.0 では、 wagtail.wagtailimages
は、wagtail.images
に、wagtail.wagtaildocs
は、wagtail.documents
に移動するようです。 ↩
コメント