(编辑添加更多代码)
这里继续收到错误,要求我将绑定方法转换为十进制。我认为问题在于这一行:
order_item = OrderItem(item=item.item_id, quantity=item.quantity, price=item.price, order=order)
我认为Django希望我将item.price转换为小数,但我一直无法弄清楚如何。我尝试了decimal.Decimal(str(item.price))哪个不起作用,而float(item.price)也没有用。一如既往,提前谢谢。
#cart.get_cart_items
def get_cart_items(request):
return Cart.objects.filter(cart_id=_cart_id(request))
#models
class Cart(models.Model):
cart_id = models.CharField(max_length=50)
item_id = models.ForeignKey('store.Item', unique=False)
date_added = models.DateTimeField(auto_now_add=True)
quantity = models.IntegerField(default=1)
class Item(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=75)
slug = models.SlugField(max_length=50, unique=True)
is_active = models.BooleanField(default=True, blank=True)
image1 = models.ImageField(upload_to='img')
image2 = models.ImageField(upload_to='img', blank=True)
image3 = models.ImageField(upload_to='img', blank=True)
image_caption1 = models.CharField(max_length=200, blank=True)
image_caption2 = models.CharField(max_length=200, blank=True)
image_caption3 = models.CharField(max_length=200, blank=True)
price = models.DecimalField(max_digits=8, decimal_places=2)
quantity = models.IntegerField(default=1)
description = models.TextField()
created = models.DateTimeField(auto_now_add=True)
shipping_price = models.DecimalField(decimal_places=2, max_digits=6)
categories = models.ManyToManyField(Category)
#views
def express_payment(request):
user = request.user
cart_subtotal = cart.cart_subtotal(request)
if request.method == "POST":
form = PaymentForm(request.POST)
if form.is_valid():
order = form.save(commit=False)
order.buyer = request.user
order.transaction_id = "12345678901234567890"
order.save()
if order.pk:
cart_items = cart.get_cart_items(request)
for item in cart_items:
order_item = OrderItem(item=item.item_id, quantity=item.quantity, price=item.price, order=order)
order_item.save()
答案 0 :(得分:0)
请提供更多错误信息,否则我们无法提供任何意义帮助..
编辑:
假设class Item
和OrderItem
是相同的,我认为user
,name
是保存OrderItem
实例所必需的。< / p>