当我尝试将商品添加到购物车中时,我正在创建一个电子商务,它会返回上述错误?
它在视图中抱怨这一行代码:
else:
order.items.add(order_item)
查看
def add_to_cart(request, slug):
item = get_object_or_404(Item, slug=slug)
order_item = OrderItem.objects.get_or_create(
item=item,
user = request.user,
ordered = False
)
order_qs = Order.objects.filter(user=request.user, ordered=False)
if order_qs.exists():
order = order_qs[0]
#check if the order item is in the order
if order.items.filter(item__slug=item.slug).exists():
order_item.quantity += 1
order_item.save()
else:
order.items.add(order_item)
else:
ordered_date = timezone.now()
order = Order.objects.create(user=request.user, ordered_date=ordered_date)
order.items.add(order_item)
return redirect("core:product", slug=slug)
型号
class OrderItem(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
ordered = models.BooleanField(default=False)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
def __str__(self):
return f"{self.quantity} of {self.item.title}"
class Order(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
items = models.ManyToManyField(OrderItem)
start_date = models.DateTimeField(auto_now_add= True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
def __str__(self):
return self.user.username
追踪
Internal Server Error: /add-to-cart/pants-2/
Traceback (most recent call last):
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/core/views.py", line 38, in add_to_cart
order.items.add(order_item)
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 965, in add
through_defaults=through_defaults,
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 1092, in _add_items
'%s__in' % target_field_name: new_ids,
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/db/models/query.py", line 892, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1290, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1318, in _add_q
split_subq=split_subq, simple_col=simple_col,
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1251, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1116, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/db/models/lookups.py", line 20, in __init__
self.rhs = self.get_prep_lookup()
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/db/models/fields/related_lookups.py", line 59, in get_prep_lookup
self.rhs = [target_field.get_prep_value(v) for v in self.rhs]
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/db/models/fields/related_lookups.py", line 59, in <listcomp>
self.rhs = [target_field.get_prep_value(v) for v in self.rhs]
File "/home/vince/Desktop/Dev-Projects/djangoEcommerce/django_project_boilerplate/env/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 966, in get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'OrderItem'
[24/Jun/2019 20:49:06] "GET /add-to-cart/pants-2/ HTTP/1.1" 500 150701
我需要它在购物车中创建一个新物品(如果它不存在),并且如果它存在于购物车中,它会将相似的物品增加1,而不是重新创建相同的物品。
答案 0 :(得分:0)
Django get_or_create(..)
[Django-doc]不会不返回模型实例,它返回带有对象的2元组和一个布尔值(无论是否创建记录)。或按照文档中的说明:
(..)
返回
(object, created)
的元组,其中object
是 检索或创建的对象,并且created
是一个布尔值,指定 是否创建了新对象。(..)
您可以使用可迭代的拆包轻松解决此问题:
def add_to_cart(request, slug):
item = get_object_or_404(Item, slug=slug)
order_item, __ = OrderItem.objects.get_or_create(
item=item,
user = request.user,
ordered = False
)
order_qs = Order.objects.filter(user=request.user, ordered=False)
if order_qs.exists():
order = order_qs[0]
#check if the order item is in the order
if order.items.filter(item__slug=item.slug).exists():
order_item.quantity += 1
order_item.save()
else:
order.items.add(order_item)
else:
ordered_date = timezone.now()
order = Order.objects.create(user=request.user, ordered_date=ordered_date)
order.items.add(order_item)
return redirect("core:product", slug=slug)
因此,我们在此将OrderItem.objects.get_or_create(..)
的结果分配给order_item, __
,其中__
是“丢弃变量”。
答案 1 :(得分:0)
将以下行添加到此。只需添加“创建的”,如下所示。
order_item, created = OrderItem.objects.get_or_create(
item=item,
user = request.user,
ordered = False
)