Home > Software engineering >  size date not saving in cartitem model but the product is saving.. Any solution how to save the size
size date not saving in cartitem model but the product is saving.. Any solution how to save the size

Time:08-24

views.py

def add_to_cart(request, pk):

    variant = request.GET.get('variant')
    
    product = Product.objects.get(pk =pk)
    user = request.user
    cart , _ = Cart.objects.get_or_create(user = user, is_paid = False)
    cart_item = CartItem.objects.create(cart = cart , product = product ,)

    if variant:
        variant = request.GET.get('variant')
        size_variant = SizeVariant.objects.get(size_name = variant)
        color_variant = ColorVariant.objects.get(color_name = variant)
        cart_item.color_variant = color_variant
        cart_item.size_variant = size_variant
        cart_item.save()

    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

models.py


class CartItem(models.Model):
    cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    color_variant = models.ForeignKey(ColorVariant, on_delete=models.CASCADE,null=True, blank=True)
    size_variant = models.ForeignKey(SizeVariant, on_delete=models.CASCADE ,null=True, blank=True)
    quantity = models.PositiveIntegerField(default=0)
    coupon = models.ForeignKey(Coupon, on_delete=models.SET_NULL, null=True, blank=True)

enter image description here

[22/Aug/2022 16:17:38] "GET /account/add_to_cart/1/?variant= HTTP/1.1" 302 0

XXX

16049.0

class SizeVariant(models.Model):
    size_name = models.CharField(max_length=100)
    price = models.IntegerField(default=0)

    def __str__(self):
        return self.size_name 

------------------------------------------------------------///////

CodePudding user response:

I don't know what's the exact problem, but I'd recommend to use get_object_or_404(), and also creating instance directly using create() instead of first creating then updating, so try below view:

def add_to_cart(request, pk):

    variant = request.GET.get('variant')
    
    product = get_object_or_404(Product,pk=pk)
    user = request.user
    cart , _ = Cart.objects.get_or_create(user = user, is_paid = False)

    if variant:
        size_variant = get_object_or_404(SizeVariant,size_name=variant)
        color_variant = get_object_or_404(ColorVariant,color_name=variant)
        CartItem.objects.create(cart = cart, product = product,color_variant=color_variant,size_variant=size_variant)

    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

CodePudding user response:

Get the id of variant instead of name. Just send the id with ajax like how you got the same object's name.

def add_to_cart(request, pk):

    variant = request.GET.get('variant')
    
    product = Product.objects.get(pk =pk)
    user = request.user
    cart , _ = Cart.objects.get_or_create(user = user, is_paid = False)
    cart_item = CartItem.objects.create(cart = cart , product = product ,)

    if variant:
        variant_id = request.GET.get('variant_id')
        size_variant = SizeVariant.objects.get(id=variant_id)
        color_variant = ColorVariant.objects.get(id=variant_id)
        cart_item.color_variant = color_variant
        cart_item.size_variant = size_variant
        cart_item.save()

    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

I can help you out about ajax if you can share the ajax side of this page.

  • Related