使用api view的方式返回数据

pip 安装包

asn1crypto==0.24.0
certifi==2018.11.29
cffi==1.11.5
chardet==3.0.4
coreapi==2.3.3
coreschema==0.0.4
cryptography==2.5
defusedxml==0.5.0
diff-match-patch==20181111
Django==2.1.5
django-crispy-forms==1.7.2
django-filter==2.1.0
django-formtools==2.1
django-guardian==1.4.9
django-import-export==1.2.0
djangorestframework==3.9.1
et-xmlfile==1.0.1
future==0.17.1
httplib2==0.12.0
idna==2.8
itypes==1.1.0
jdcal==1.4
Jinja2==2.10
Markdown==3.0.1
MarkupSafe==1.1.0
odfpy==1.4.0
openpyxl==2.5.14
optionaldict==0.1.1
Pillow==5.4.1
pycparser==2.19
PyMySQL==0.9.3
python-dateutil==2.7.5
pytz==2018.9
PyYAML==3.13
requests==2.21.0
six==1.12.0
tablib==0.12.1
unicodecsv==0.14.1
uritemplate==3.0.0
urllib3==1.24.1
wechatpy==1.7.6
xlrd==1.2.0
xlwt==1.3.0
xmltodict==0.11.0

drf 代码:

from django.urls import path
from rest_framework.documentation import include_docs_urls
from rest_framework.routers import DefaultRouter

# router = DefaultRouter()

urlpatterns = [
    # path('api-auth/', include('rest_framework.urls')),
    # path('api_books/', include(router.urls)),
    path('api-auth/', include('rest_framework.urls')),
    path('docs/', include_docs_urls(title='生鲜超市')),
]

goods view

from goods.models import Goods
from goods.serializers import GoodsSerializer
from rest_framework.views import APIView
from rest_framework.response import Response

class GoodsListView(APIView):
  def get(self, request):
    goods = Goods.objects.all()
    goods_serializer = GoodsSerializer(goods, many=True)
    return Response(goods_serializer.data)

goods serializers

from rest_framework import serializers

class GoodsSerializer(serializers.Serializer):
  name = serializers.CharField(required=True, max_length=100)
  click_num = serializers.IntegerField(default=0)

你可能感兴趣的:(使用api view的方式返回数据)