Django:不可迭代对象到 QuerySet

我正在尝试将一个不可迭代的对象解压到一个 QuerySet 中。这是我的代码:


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.get(id=id_order, membre_id=id_user)

        else:

            orders = commande.objects.filter(membre_id=id_user)

                              ...

我想在收到请求时解包订单...?user=1&order=2。提前致谢


泛舟湖上清波郎朗
浏览 99回答 1
1回答

温温酱

from django.shortcuts import render, get_object_or_404from 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() ,如果它为空则引发错误
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python