我有一个Django购物车项目,可以从购物车中添加和删除产品。我似乎无法锻炼如何向购物车添加数量(每个产品在购物车中的次数)。我知道我需要在模型中添加数量字段。但是我在哪里更改逻辑 - 在实际模型或视图上?
Models.py(包括经理)
from django.conf import settings
from django.db import models
from django.db.models.signals import pre_save, post_save, m2m_changed
from catalogue.models import Product
User = settings.AUTH_USER_MODEL
# Create your models here.
class CartManager(models.Manager):
def new_or_get(self, request):
cart_id = request.session.get("cart_id", None)
qs = self.get_queryset().filter(id=cart_id)
if qs.count() == 1:
new_obj = False
cart_obj = qs.first()
if request.user.is_authenticated() and cart_obj.user is None:
cart_obj.user = request.user
cart_obj.save()
else:
cart_obj = Cart.objects.new(user=request.user)
new_obj = True
request.session['cart_id'] = cart_obj.id
return cart_obj, new_obj
def new(self, user=None):
user_obj = None
if user is not None:
if user.is_authenticated():
user_obj = user
return self.model.objects.create(user=user_obj)
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
products = models.ManyToManyField(Product, blank=True)
# quantity = models.IntegerField()
total = models.DecimalField(default=0.00, max_digits=10, decimal_places=2)
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)
objects = CartManager()
def __str__(self):
return str(self.id)
def pre_save_cart_receiver(sender, instance, action, *args, **kwargs):
if action == 'post_add' or action == 'post_remove' or action == 'post_clear':
products = instance.products.all()
total = 0
for x in products:
total += x.price
instance.total = total
instance.save()
m2m_changed.connect(pre_save_cart_receiver, sender=Cart.products.through)
Views.py
from django.http import JsonResponse
from django.shortcuts import render, redirect
from catalogue.models import Product
from .models import Cart
# Create your views here.
def cart_detail_api_view(request):
cart_obj, new_obj = Cart.objects.new_or_get(request)
products = [{"name": x.name, "price": x.price} for x in cart_obj.products.all()]
cart_data = {"products": products, "total": cart_obj.total}
return JsonResponse(cart_data)
def cart_home(request):
cart_obj, new_obj = Cart.objects.new_or_get(request)
return render(request, "carts/carts.html", {"cart": cart_obj})
def cart_update(request):
product_id = request.POST.get('product_id')
if product_id is not None:
try:
product_obj = Product.objects.get(id=product_id)
except Product.DoesNotExist:
print("Show message to user, product is gone?")
return redirect("cart:home")
cart_obj, new_obj = Cart.objects.new_or_get(request)
if product_obj in cart_obj.products.all():
cart_obj.products.remove(product_obj)
added = False
else:
cart_obj.products.add(product_obj) # cart_obj.products.add(product_id)
added = True
request.session['cart_items'] = cart_obj.products.count()
# return redirect(product_obj.get_absolute_url())
if request.is_ajax(): # Asynchronous JavaScript And XML / JSON
print("Ajax request")
json_data = {
"added": added,
"removed": not added,
}
return JsonResponse(json_data)
return redirect("cart:home")
答案 0 :(得分:1)
不要添加IntegerField
!每次向购物车添加新商品时,您都必须确保手动增加它,并在每次删除商品时减少。相反,您可以使用count()
method:
def cart_detail_api_view(request):
cart_obj, new_obj = Cart.objects.new_or_get(request)
products = [{"name": x.name, "price": x.price} for x in cart_obj.products.all()]
cart_data = {
"products": products,
"total": cart_obj.total,
"count": cart_obj.products.count()
}
return JsonResponse(cart_data)
你应该看一下Django REST Framework - 它会让你做的很多事情变得更容易,更精简,更干。