创建视图
前面两篇文章中介绍了如何使用django创建模型,本文将对创建简单的视图进行说明,这里使用DRF(django restframework),它为django提供了REST接口,使设计人员能够简单快速的设计出RESTFUL风格的接口
一、编写serializer
- 新建serializers.py文件
- 编写作者列表、作者信息、诗词列表和诗词信息序列化代码。作者列表显示id、作者名称和朝代;作者信息和诗词信息显示所有model内容(fields = “all”);诗词列表增加了做着名称和朝代显示。
from rest_framework import serializers
from .models import Author, Poem
# 作者列表
class AuthorListSerializer(serializers.ModelSerializer):
class Meta:
model = Author
fields = ("id", "name", "dynasty", )
# 作者信息
class AuthorReadSerializer(serializers.ModelSerializer):
class Meta:
model = Author
fields = "__all__"
# 诗词列表
class PoemListSerializer(serializers.ModelSerializer):
author_name = serializers.CharField(source='author.name', allow_null=True)
poem_dynasty = serializers.CharField(source='author.dynasty', allow_null=True)
class Meta:
model = Poem
fields = ("id", "title", "author_name", "author", "poem_dynasty", )
# 诗词信息
class PoemReadSerializer(serializers.ModelSerializer):
author_name = serializers.CharField(source='author.name', allow_null=True)
poem_dynasty = serializers.CharField(source='author.dynasty', allow_null=True)
class Meta:
model = Poem
fields = "__all__"
二、编辑View.py
- 添加Author列表和信息视图接口
AuthorListPagination继承了PageNumberPagination实现了列表的分页
class AuthorListPagination(PageNumberPagination):
page_size = 50
page_size_query_param = 'page_size'
page_query_param = 'page'
max_page_size = 200
class AuthorListView(viewsets.GenericViewSet, mixins.ListModelMixin):
'''
list:
'''
pagination_class = AuthorListPagination
serializer_class = AuthorListSerializer
queryset = Author.objects.all()
class AuthorReadView(viewsets.GenericViewSet, mixins.RetrieveModelMixin):
'''
retrieve:
'''
serializer_class = AuthorReadSerializer
queryset = Author.objects.all()
- 同样的添加Poem的列表和信息视图接口
class PoemListPagination(PageNumberPagination):
page_size = 50
page_size_query_param = 'page_size'
page_query_param = 'page'
max_page_size = 200
class PoemListView(viewsets.GenericViewSet, mixins.ListModelMixin):
'''
list:
'''
pagination_class = PoemListPagination
serializer_class = PoemListSerializer
queryset = Poem.objects.all()
class PoemReadView(viewsets.GenericViewSet, mixins.RetrieveModelMixin):
'''
retrieve:
'''
serializer_class = PoemReadSerializer
queryset = Poem.objects.all()
三、其他设置
- 设置url
在urls.py文件中添加代码,将url和视图进行关联, author/list对应AuthorListView,author/read对应AuthorReadView,poem/list对应PoemListView,poem/read对应PoemReadView,启用drf的
api文档,对应地址api
from django.urls import path, re_path, include
from rest_framework.documentation import include_docs_urls
from rest_framework.routers import DefaultRouter
from poem.views import AuthorListView, AuthorReadView, PoemListView, PoemReadView
router = DefaultRouter()
router.register(r'author/list', AuthorListView)
router.register(r'author/read', AuthorReadView)
router.register(r'poem/list', PoemListView)
router.register(r'poem/read', PoemReadView)
urlpatterns = [
# path('admin/', admin.site.urls),
# path('api-auth/', include('rest_framework.urls')),
path('api', include_docs_urls(title='诗词项目')),
re_path('^', include(router.urls)),
]
- 在setting.py中添加如下代码
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
}
四、测试