(编辑添加更多代码)
在这里不断出现错误,要求我将绑定方法转换为十进制。我认为问题在于这一行:
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()