졔졔류류
80
2019-10-23 00:52:37
2
332

Django MultipleObjectsReturned at /order/create/ get() returned more than one Product -- it returned 3!


from django.shortcuts import render
from order.models import OrderItem, Order
from product.models import Product
from accounts.models import User
from order.forms import OrderCreateForm
from cart.cart import Cart

def order_create(request):
cart = Cart(request)
if request.method == 'POST':
form = OrderCreateForm(request.POST)
if form.is_valid():
# profile = User.objects.get(user=request.user)
profile = User.objects.get(pk=request.user.pk)
form.instance.user = request.user
order = form.save()
profile.address = request.POST.get('address')
profile.save()
for item in cart:
OrderItem.objects.create(
order = order,
user = request.user,
product = item['product'],
price = item['price'],
quantity = item['quantity'],
)
products = Product.objects.get(p_name=item['product'])
products.p_stock -= item[
'quantity']
products.order_q += item[
'quantity']
products.save()

# userinfo = User.objects.get(user=request.user)
userinfo = User.objects.get(pk=request.user.pk)
userinfo.point = userinfo.point + products.p_point * item[
'quantity'] - order.usepoint
userinfo.save()

cart.clear()

return render(request, 'order/order_created.html', {'order': order})

else:
form = OrderCreateForm()
context = {
'form': form,
'cart': cart,
}
return render(request, 'order/order_create.html', context=context)
제가 형광펜 칠한 부분에서 MultipleObjectsReturned 오류가 납니다. 어떻게 해결해야되는지 아시는 분 도와주세요..!
0
0
  • 답변 2

  • shaffron
    3k
    2019-10-23 10:05:37
    products = Product.objects.filter(p_name=item['product']).first()
    1
  • 졔졔류류
    80
    2019-10-24 15:34:05

    답변 감사합니다! 그런데 저렇게 get을 filter로 수정하면  바로 아랫 줄인 products.p_stock -= item['quantity'] 부분에서 

    AttributeError at /order/create/

    'QuerySet' object has no attribute 'p_stock'

    오류가 나는데 어떻게 해야될까요ㅠ!?

    0
  • 로그인을 하시면 답변을 등록할 수 있습니다.