When I try add the product to wishlist it gives me above error, It still saves the object to model
def plus_wishlist(reques):
if request.method == "GET":
prod_id = request.GET.get('prod_id')
product = Product.objects.get(id=prod_id)
ruser = request.user
wishlist = Wishlist.objects.create(user=ruser, product=product)
wishlist.save()
return JsonResponse({'message': 'Added to Wishlist.'})
Also when i try to use
ruser=request.user.id
ValueError: Cannot assign "1": "Wishlist.user" must be a "User" instance.
class Wishlist(models.Model):
user=models.ForeignKey(User,on_delete=models.CASCADE)
product=models.ForeignKey(Product,on_delete=models.CASCADE)
Issue is Solved!!
ruser = get_object_or_404(User,id=request.user.id)
I dont know if there was login error or what, after getting same error I cleared localhost url and it took me to login page after login there was no issue
Limit views to a view to authenticated users with the
@login_requireddecorator [Django-doc]:We can also boost efficiency with:
That being said, you can not add an item to a
Wishlistwith a GET request: GET requests should have no side-effects. Adding something to aWishlistthus should be done with a POST request.