1 回答
TA贡献1752条经验 获得超4个赞
from django.shortcuts import render, get_object_or_404
from rest_framework.response import Response
...
@api_view(['GET'])
def order(request):
'''Get all the orders for a user'''
context = []
if request.GET.get('user'): #If we make a request type ?user=...
id_user = request.GET.get('user')
user = get_object_or_404(member_area, pk=id_user)
if request.GET.get('order'): #If we make specific request on an order
id_order = request.GET.get('order')
orders = commande.objects.filter(id=id_order, membre_id=id_user)
if not orders.exists():
return HttpResponse(status=500) #Or 404
else:
orders = commande.objects.filter(membre_id=id_user)
您可以使用 .filter() 而不是 .get() ,如果它为空则引发错误
添加回答
举报