У меня есть корзина, продукт и модель входа.То, что я пытаюсь сделать, это дать пользователям возможность добавить более одного товара в корзину и отобразить количество в кассе.Я могу получить выбранное количество свыше
quantity_input= request.POST.get('quantity-field')
и создать новый объект Entry внутри cart_update ()
Entry.objects.create(cart=cart_obj, product=product_obj, quantity=quantity_form)
, который знает, к какой корзине и продукту он принадлежит.Но затем я ударил стену, выводя ее поверх вида, так как у меня есть только cart_obj в качестве контекста, не зная, как дополнительно визуализировать объект ввода.
Модель корзины:
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
products = models.ManyToManyField(Product, blank=True)
subtotal = models.DecimalField(default=0.00, decimal_places=2, max_digits=100)
total = models.DecimalField(default=0.00, decimal_places=2, max_digits=100)
count = models.PositiveIntegerField(default=0)
objects = CartManager()
Модель входа
class Entry(models.Model):
product = models.ForeignKey(Product, null=True)
eCart = models.ForeignKey(Cart, null=True)
quantity = models.PositiveIntegerField()
@receiver(post_save, sender=Entry)
def update_cart(sender, instance, **kwargs):
line_cost = instance.quantity * instance.product.price
instance.cart.count = int(instance.cart.count) + int(instance.quantity)
тележки views.py
def cart_update(request):
product_id = request.POST.get('product_id')
quantity_input= request.POST.get('quantity-field')
if product_id is not None:
cart_obj, new_obj = Cart.objects.new_or_get(request)
Entry.objects.create(cart=cart_obj, product=product_obj, quantity=quantity_input)
cart_obj.products.add(product_obj)
added = True
request.session['cart_items'] = cart_obj.products.count()
return redirect("carts:home")
def cart_home(request):
cart_obj, new_obj = Cart.objects.new_or_get(request)
return render(request, "carts/home.html",{"cart":cart_obj})
Спасибо за помощь.