'problem with a field that expected a number but got another thing
this is the error im using django 4.0.2 and making a cart but when i make the check out, i get this error any suggestion to make it work? i appreciate your help
i have in checkout.py this code
class Checkout(LoginRequiredMixin, View):
def post (self, request):
addres = request.POST.get('addres')
comment = request.POST.get('comment')
cart = request.session.get('cart')
user= request.session.get('user')
product = Product.get_product_by_id(cart)
print(addres, comment, User, cart, product )
for product in product:
print(cart.get(str(product.id)))
order = Order(user_id=user,
product=product,
price=product.price,
addres=addres,
comment=comment,
quantity=cart.get(str(product.id)))
order.save()
request.session['cart'] = {}
return redirect ('cart:cart_detail')
#i have a cart.py code to that have some values, but i can't see where is the problem i'm following some tutorials to do this code. my cart.py
from django.conf import settings
from product.models import Product
class Cart(object):
def __init__(self, request):
self.session = request.session
cart = self.session.get(settings.CART_SESSION_ID)
if not cart:
cart = self.session[settings.CART_SESSION_ID] = {}
self.cart = cart
def add(self, product):
if str(product.id) not in self.cart.keys():
self.cart[product.id]={
"product_id": product.id,
"title":product.title,
'quantity': 1, 'id': int(product.id),
"image":product.image.url,
"thumbnail":product.thumbnail.url,
"price": str(product.price)
}
else:
for key, value in self.cart.items():
if key== str(product.id):
value["quantity"] = value["quantity"]+ 1
break
self.save()
def save(self):
self.session["cart"] = self.cart
self.session.modified = True
def remove(self, product):
product_id = str(product.id)
if product_id in self.cart:
del self.cart[product_id]
self.save()
def decrement(self, product):
for key, value in self.cart.items():
if key == str(product.id):
value["quantity"] = value["quantity"]- 1
if value["quantity"] < 1:
self.remove(product)
else:
self.save()
break
else:
print("the item does not exist in the cart")
def clear(self):
self.session["cart"] = {}
self.session.modified = True
this is my user model
from enum import unique
from statistics import mode
from django.conf import settings
from django.db import models
from django.contrib.auth.models import AbstractUser
from limpiaC.settings import MEDIA_URL, STATIC_URL
class Gender(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
class User(AbstractUser):
identification = models.CharField(max_length=255, unique=True)
image = models.ImageField(upload_to='profile_uploads/', blank=True, null=True,default='images/defaultProfile.jpg')
gender = models.ForeignKey(Gender, related_name='genders', on_delete=models.CASCADE)
company = models.CharField(max_length=255)
state = models.CharField(max_length=255)
zip = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
date_added = models.DateTimeField(auto_now_add=True)
dateofbirth = models.DateField
def get_image(self):
if self.image:
return '{}{}'.format(MEDIA_URL, self.image)
return '{}{}'.format(STATIC_URL, 'images/defaultProfile.jpg')
Solution 1:[1]
In this part of code:
order = Order(user_id=user,
product=product,
price=product.price,
addres=addres,
comment=comment,
quantity=cart.get(str(product.id))) # HERE
You are accessing the whole cart object (which is saved as JSON or dict?)
You need to access just quantity field so use:
order = Order(user_id=user,
product=product,
price=product.price,
addres=addres,
comment=comment,
quantity=cart.get(str(product.id))["quantity"]) # <--
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bartosz Stasiak |
