Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Django Cart and Item Model - getting quantity to update

Rutnet:

I am working on a Django Cart application. I have two models Cart and Item. I am trying to get the quantity to update when an Item is added to the basket but cant get the views to work properly. I am having problems getting item_obj assignment to work - do I need to do anything with the model manager here? Any help is really appreciated.

Models.py


class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
products = models.ManyToManyField(Product, blank=True)
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)

class Item(models.Model):
item = models.ForeignKey(Product, null=True)
cart = models.ForeignKey(Cart, null=True)
quantity = models.PositiveIntegerField()

Views.py extract


def cart_update(request):
product_id = request.POST.get('product_id')
product_obj = Item.objects.get(id=product_id)
print(item_id)
item_obj = Item.objects.get(id=product_id)
cart_obj, new_obj = Cart.objects.new_or_get(request)
if item_obj in cart_obj.products.all():
cart_obj.products.add(product_obj)
item_obj.quantity += 1
item_obj.save()
else:
cart_obj.products.add(product_obj)
return redirect("cart:home")



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Django Cart and Item Model - getting quantity to update

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×