Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Django-REST-framework 基本教學 - 從無到有 DRF-Beginners-Guide 📝

License

Notifications You must be signed in to change notification settings

twtrubiks/django-rest-framework-tutorial

 
 

Repository files navigation

django-rest-framework-tutorial

Django-REST-framework 基本教學 - 從無到有 DRF-Beginners-Guide 📝

因為 Django > 2.0 改動蠻多了,所以這邊會把一些和 Django < 2.0 不一樣的地方寫出來。

Django 以及 DRF 的版本請參考 requirements.txt

Django==2.2.1
djangorestframework==3.9.3

教學

建立 project ( 此步驟只須執行一次,所以不用在執行了 )

django-admin startproject django_rest_framework_tutorial .

後面的 . 代表在目錄底下的意思。

django-rest-framework 設定

請記得要將 Django-REST-framework 加入設定檔

請在 settings.py 裡面的 INSTALLED_APPS 加入下方程式碼 (下圖)

INSTALLED_APPS = (
    ...
    'rest_framework',
    ...
)

建立 Django App

建立一個 App ,

請在你的命令提示字元 (cmd ) 底下輸入

python manage.py startapp musics

建立完請記得要將 App 加入設定檔

請在 settings.py 裡面的 INSTALLED_APPS 加入設定,

注意:exclamation:這邊和 Django < 2.0 不太一樣,

INSTALLED_APPS = [
    .....
    'rest_framework',
    'musics.apps.MusicsConfig'
]

Models

定義 models.py

接著在命令提示字元 (cmd ) 底下輸入

python manage.py makemigrations musics

python manage.py migrate

Serializers 序列化

定義 serializers.py

Views

定義 views.py

Routers 路由

注意:exclamation:這邊和 Django < 2.0 不太一樣。

定義 django_rest_framework_tutorial/urls.py

基本上就是改成使用 path

from django.contrib import admin
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from musics.views import MusicViewSet

router = DefaultRouter()
router.register('musics', MusicViewSet)

urlpatterns = [
    # path('admin/', admin.site.urls),
    # for rest_framework
    path('api/', include(router.urls)),
    # for rest_framework auth
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

Extra actions for routing

注意:exclamation:這邊和 Django < 2.0 不太一樣。

Django < 2.0 時,是使用 @detail_route@list_route

但在 Django > 2.0中,統一都使用 @action decorator

搭配 detail argument 參數 ( True or False)。

範例程式碼可參考 views.py

@action decorator 搭配 detail=True ( 就是以前的 @detail_route )

# [GET] /api/musics/{pk}/detail/
@action(detail=True, methods=['get'], url_path='detail')
def detail_action(self, request, pk=None):
    music = get_object_or_404(Music, pk=pk)
    result = {
        'singer': music.singer,
        'song': music.song
    }
    return Response(result, status=status.HTTP_200_OK)

以上面這個例子來說, URL pattern 為 /api/music/{pk}/detail/

@action decorator 搭配 detail=False ( 就是以前的 @list_route )

# [GET] /api/musics/all_singer/
@action(detail=False, methods=['get'], url_path='all_singer')
def all_singer(self, request):
    music = Music.objects.values_list('singer', flat=True).distinct()
    return Response(music, status=status.HTTP_200_OK)

以上面這個例子來說,URL pattern 為 /api/music/all_singer/

更多資訊可參考 https://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing

後記

這篇 Django > 2.0 的文章,我會慢慢更新,如果我發現一些寫法或功能,我會更新在這篇。

執行環境

  • Python 3.6.6

Reference

Donation

文章都是我自己研究內化後原創,如果有幫助到您,也想鼓勵我的話,歡迎請我喝一杯咖啡:laughing:

alt tag

贊助者付款

License

MIT license

About

Django-REST-framework 基本教學 - 從無到有 DRF-Beginners-Guide 📝

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.