关于用复合类型的查询
来源:1-1 本周学习安排
南柯几梦
2020-09-29 15:36:02
根据教程用的复合类型,如何让前端在调用get请求的时候将对应的content_type,我们用的django rest_framework 实现的api
4回答
时间,
2020-09-29
同学,你好。
一、参考代码:
1、views.py
from django.http import JsonResponse
from rest_framework.response import Response
from trip.models import Order, Comment
from rest_framework.views import APIView
class CommAPIView(APIView):
def get(self, request, *args, **kwargs):
order = Order.objects.filter(pk=1).first()
result = order.comments.filter(pk=1).first()
print(result)
resp = {
'sn':order.sn,
'amount':result.content
}
return Response(resp)
# 添加信息
def post(self, request, *args, **kwargs):
sn = request.GET.get("sn")
print("sn",sn)
amount = request.GET.get('amount')
content = request.GET.get('amount')
score = request.GET.get('amount')
order = Order.objects.create(sn=sn, amount=amount)
common = Comment.objects.create(content_object=order, content=content, score=score)2、urls.py
from trip.views import CommAPIView urlpatterns = [ url(r'^common$', CommAPIView.as_view(), name='add'), ]
3、返回的是json类型的数据:get请求:http://127.0.0.1:8002/trip/common

post:请求 http://127.0.0.1:8002/trip/common?sn=36&amount=990&content="content34"&score=54 在url中拼接所需要的数据
二、分页功能同学可结合之前课程中讲过的分页来实现
如果我的回答解决请你的疑惑采纳!请采纳!祝学习愉快~~~~
南柯几梦
提问者
2020-09-29
还有一个问题是多表拼凑出来的数据如何做分页,用切片吗
南柯几梦
提问者
2020-09-29
能请教一下在django REST framework 做到吗,用它的api实现,或者自己写一个带分页功能的
时间,
2020-09-29
同学,你好。同学可以根据下述方法实现
1、先访问add()添加相应的数据,再访问comm()查询相应的数据并渲染到html中,即可在页面中看到相应的数据




如果我的回答解决了你的疑惑,请采纳!祝学习愉快~~~~
相似问题