To build rest api easily, we can use ModelViewSet from rest_framework.
It provides GET, POST, DELETE, PUT methods.
from rest_framework.viewsets import ModelViewSet from .serializers import ListSerializer, CardSerializer from .models import List, Card class ListViewSet(ModelViewSet): queryset = List.objects.all() serializer_class = ListSerializer class CardViewSet(ModelViewSet): queryset = Card.objects.all() serializer_class = CardSerializer
To render those model view set, you can use router provided by rest_framework.
By default, it end with slash!
from .api import ListViewSet, CardViewSet from rest_framework.routers import DefaultRouter router = DefaultRouter() router.register(r‘lists‘, ListViewSet) router.register(r‘cards‘, CardViewSet) urlpatterns = router.urls
时间: 2024-10-19 19:34:26