Я пытаюсь добавить новую функцию электронной коммерции в свой проект python / django и продолжаю получать сообщение об ошибке при использовании кнопки «Добавить в корзину». Я очень расстроен, потому что я боролся с этим уже несколько недель, и мне действительно нужна помощь. Заранее спасибо.
вот модель EquipmentOrder
class EquipmentOrder(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
equipment = models.ManyToManyField(EquipmentOrderItem)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
objects = models.Manager()
def __str__(self):
return self.user.username
вот вид в корзину
def add_to_cart(request, slug):
equipment = get_object_or_404(models.Equipment, slug=slug)
equipment_order_item, created = models.EquipmentOrderItem.objects.get_or_create(
equipment=equipment,
user=request.user,
ordered=False
)
equipment_order_qs = models.EquipmentOrder.objects.filter(user=request.user, ordered=False)
if equipment_order_qs.exists():
equipment_order = equipment_order_qs[0]
# check if equipment order item is in order
if equipment_order.equipment.filter(equipment__slug=equipment.slug).exists():
equipment_order_item.quantity += 1
equipment_order_item.save()
messages.info(request, "This item quantity was updated.")
else:
messages.info(request, "This item was added to your cart.")
equipment_order.equipments.add(equipment_order_item)
else:
ordered_date = timezone.now()
equipment_order = models.EquipmentOrder.objects.create(user=request.user, ordered_date=ordered_date)
equipment_order.equipments.add(equipment_order_item)
messages.info(request, "This item was added to your cart.")
return redirect("create:equipment_detail", slug=slug)