标签:setting 必须 定义 serialize 注册 组件 edm cts style
下载 restframework
新建django项目,有事APP,所以在setting中注册额app
创建表(Book),数据库迁移
urls.py:
from app01 import views urlpatterns = [ url(r"^books/",views.BookView.as_view()) #CBV ]
注册:router.register(r‘books‘,views.BookViewSet)
views中创建视图类:
from app01 import models
from app01 import models
Class BookView(viewsets.ModelViewSet): #必须继承
queryset=models.Book.objects.all() serializer_class = BookSerializer #之前定义好的BookSerializer组件
视图中用的 BookSerializer:
class BookSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = models.Book fields = (‘url‘, ‘title‘)
标签:setting 必须 定义 serialize 注册 组件 edm cts style
原文地址:https://www.cnblogs.com/pygg/p/9201716.html