0

ページを読み込もうとすると、このエラーが発生しました。get() returned more than one Item -- it returned 2! Lookup parameters were {}

ページをレンダリングするビューは次のとおりです。

def get_category(request):
    categories = Category.objects.all().prefetch_related('item')
    # need to evaluate the HTTP method
    if request.method == 'POST':
        # add to order..create the bound form
        postdata = request.POST.copy()
        form = forms.PartialOrderItemForm(request.POST,postdata)
        # check validation of posted data
        if form.is_valid():

            order.add_to_order(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url =urlresolvers.reverse('order_index')
            # redirect to order page
            return HttpResponseRedirect(url)
    else:
        # it's a GET, create the unbound from. Note request as a Kwarg
        form = forms.PartialOrderItemForm(request.GET)
    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    context={
        'categories':categories,
        'form':form,
#       'menu':menu,

    }
    return render_to_response('category.html',context,context_instance=RequestContext(request))

このビューは、 の後のある時点で次の関数を呼び出しますis_valid

def add_to_order(request):  
    postdata = request.POST.copy()
    #get quantity added, return 0 if empty
    quantity = postdata.get('quantity',0)
    # fetch the item or return  missing page error_message
    i = get_object_or_404(Item,)
    # get items in order
    order_items = get_order_items(request)
    item_in_orders = False
    # check to see if item is already in order
    for order_item in order_items:
        if order_item.item.id == i.id:
            #update the quantity if found
            order_item.augment_quantity(quantity)
            item_in_orders = True
    if not item_in_orders:
        # creat and save a new order item
        oi = OrderItem()
        oi.order_id = _order_id(request)
        oi.quantity = quantity
        oi.item = i
        oi.save()

ここにトレースバックがあります

Traceback:
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/mats-invasion/projects/f4l/f4l/live/views.py" in get_category
  26.           order.add_to_order(request)
File "/home/mats-invasion/projects/f4l/f4l/cart/order.py" in add_to_order
  42.   i = get_object_or_404(Item,)

File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/shortcuts/__init__.py" in get_object_or_404
  113.         return queryset.get(*args, **kwargs)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/db/models/query.py" in get
  368.                 % (self.model._meta.object_name, num, kwargs))

Exception Type: MultipleObjectsReturned at /menu/
Exception Value: get() returned more than one Item -- it returned 2! Lookup parameters were {}

ありがとうございました 。

4

1 に答える 1

2

あなたの行i = get_object_or_404(Item,)ではアイテムを取得しようとしますが、フィルター クエリを指定していないため、Item.objects.get()すべてのオブジェクトを返すものを試します ( と同様Item.objects.all())。指定されたクエリに複数のオブジェクトがある場合、例外が発生し.get()ますget_object_or_404()MultipleObjectsReturned

したがって、行を次のように更新する必要があります。

i = get_object_or_404(Item, id=someid)
于 2013-01-15T07:23:40.310 に答える