获取查询数据
Posted chenyang13677
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取查询数据相关的知识,希望对你有一定的参考价值。
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‘),
以上是关于获取查询数据的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Firebase 获取数据到 Recyclerview 中的片段?