Skip to content Skip to sidebar Skip to footer

Multipleobjectsreturned At /cart/ In Django

i'm a beginner and i am getting this error 'Exception Value: get() returned more than one Order -- it returned 2!' views.py def cart(request): customer=request.user.customer

Solution 1:

The above error indicatess that you have more than one record in the DB related to the specific parameter you passed while querying using get()

Solution 2:

The responsible line for this error is

order,created=Order.objects.get_or_create(customer=customer,complete=False)

because you didn't have any constraint based on this filter and this query returned more than one object and since get must return only one object, this line raised MultipleObjectReturned error type. So if you want to use get_or_create manager make sure that this filter return only one object (by setting database constraints etc). Another way is to change this line to something like:

order = Order.objects.filter(customer=customer,complete=False).first() # or .last() based on your requirements
ifnotorder:
    order = Order.objects.create(customer=customer,complete=False)

You can also read more about get_or_createhere.

Solution 3:

get returns a single object and takes in one parameter, filter returns an iterable queryset of objects and can take in multiple parameters (like the query you made)

Solution 4:

your query Order.objects.get_or_create() will only work for a single record. It is used to prevent duplicate record creation. If you want to query multiple records you can use Order.objects.filter() or Order.objects.filter().get_or_create() by using Q objects.

Solution 5:

I might be wrong but below is my understanding. You have not mentioned what exactly you are trying to achieve and this fact compounds the problem.

get_or_create() is a function primarily for lookup and it will create record only if it can't find one. See below from official docs .

A convenience method for looking up an object with the given kwargs (may be empty if your model has defaults for all fields), creating one if necessary.

So probably in your case the get method succeeds (and there is no need to create and hence it does not go to the creation step) but the get method returns more than one item. This is because, the "particular customer" has more than one record having "complete" filed equal to false. See the official docs again in the link above.

If multiple objects are found, get_or_create() raises MultipleObjectsReturned.

The only unique handle in your table might be date_orderd (and probably transaction_id, if it is correctly setup). However you are not using this field in get_or_create().

If you want to create a single object, then simply use create(). Also I think you have an error in the next line (below) since based on the code shared orderitem_set is not in the order table.

items=order.orderitem_set.all()

Slightly changed code in views is as below

defcart(request):
    customer=request.user.customer
    order,created=Order.objects.create(customer=customer,complete=False)
    items=Order.objects.all()
    context={
        'items':items
    }
    return render(request, 'catalog/cart.html',context)

Post a Comment for "Multipleobjectsreturned At /cart/ In Django"