码迷,mamicode.com
首页 > 其他好文 > 详细

获取查询数据

时间:2017-11-07 20:52:13      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:app   utf-8   model   slist   tap   获取   数据   queryset   frame   

1.在app下,新建一个文件serializers.py添加数据

# coding=utf-8
from rest_framework import serializers
from goods.models import Goods, GoodsCategory


class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = GoodsCategory
fields = ‘__all__‘


class GoodsSerializer(serializers.ModelSerializer):
category = CategorySerializer()
#实例化存在外键关系的category
class Meta:
model = Goods
fields = ‘__all__‘


2.在views.py文件里面添加:
from .serializers import GoodsSerializer
from rest_framework import generics
from .models import Goods


class GoodsListView(generics.ListAPIView):
queryset = Goods.objects.all()[:10]
serializer_class = GoodsSerializer

3.在urls.py文件里面添加

from goods.views import GoodsListView
url(r‘^goods/$‘, GoodsListView.as_view(), name=‘goods-list‘),


获取查询数据

标签:app   utf-8   model   slist   tap   获取   数据   queryset   frame   

原文地址:http://www.cnblogs.com/chenyang13677/p/7800693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!