Home > front end >  Cannot assign "'T Shirt for mens'": "CartProdVarient.cart_product" mus
Cannot assign "'T Shirt for mens'": "CartProdVarient.cart_product" mus

Time:08-03

Models

class CartProduct(models.Model):
    cart_product_name = models.CharField(max_length=200)
    cart_holder = models.ForeignKey(User, on_delete=models.CASCADE)
    def __str__(self):
        return self.cart_product

class CartProdVarient(models.Model):
    cart_product = models.ForeignKey(CartProduct, on_delete=models.CASCADE)
    cart_prod_varient = models.CharField(max_length=200)
    def __str__(self):
        return self.cart_prod_varient

Views

 def add_cart(request):
    prod = Product.objects.get(id=request.POST.get('product_id'))

    CartProdVarient(cart_product=prod.product_name).save()

    return render(request, 'app/service-page.html')

Problem i want to assign some value to "cart_product" while its a Foreign key but it giving me error..

i want something like this :

 CartProdVarient(cart_product="T-Shirt", cart_prod_varient="small size").save()

T-Shirt is already in "cart_product_name"

CodePudding user response:

Your models are working fine. And the foreign key integration and model fields are correct. There is no error in the model.

The problem is with your views :

prod = Product( id = request.POST.get("product_id") )
#  this return an product object named prod , and this is a product object. 

See your model CartProductVariant , you have defined the cart_product to be the foreignkey on the model CartProduct .


class CartProdVarient(models.Model):
    cart_product = models.ForeignKey(CartProduct, on_delete=models.CASCADE)
    cart_prod_varient = models.CharField(max_length=200)
    def __str__(self):
        return self.cart_prod_varient

Your first mistake . See your views in this line :

CartProdVarient(cart_product=prod.product_name).save()

You have defined relation with the CartProduct object but you are providing the name of the Product object . These two are different models. This might be the first case of error .

Your second mistake . Now see your views , in this line :

CartProdVarient(cart_product=prod.product_name).save()

You have defined the relation with the CartProduct , in Django whenever a foreign key is made the object is provided in the argument but you are providing the name of the product where as the cart_product will take the object of the CartProduct itself. It does not take the model fields , it takes the object itself.

change your code to this if the model CartProduct and Product are same :

CartProdVarient(cart_product=prod).save()

If the model CartProduct and Proudct are not same ( and I think they are not ) , you should change cart_product argument of Product object to CartProject object.

CartProdVarient(cart_product= CartPoduct.objects.get(id = request.POST.get("cart_product_id"))).save()

The whole code for the views need to be changed based on the model you have created and what is the relation you have defined on the models .

If the Product model and the CartProduct model are same ( and I don't think so they are ) . Use this code snippet .

def add_cart(request):
    prod = Product.objects.get(id=request.POST.get('product_id'))

    CartProdVarient(cart_product=prod).save()

    return render(request, 'app/service-page.html')

If they are different use this :

def add_cart(request):
    prod = CartProduct.objects.get(id=request.POST.get('cart_product_id'))

    CartProdVarient(cart_product=prod).save()

    return render(request, 'app/service-page.html')
  • Related