-1
我得到這個錯誤,當我做一整列的總和:無法轉換{ 'price_total__sum':十進制( '258.00')}爲十進制
Cannot convert {'price_total__sum': Decimal('258.00')} to Decimal
這裏整個項目:https://github.com/pierangelo1982/djangocommerce/tree/berge
def add_to_order(request):
if request.method == "POST":
form = AddOrderForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.published_date = timezone.now()
post.tot_price = CartItem.objects.filter(user_id=request.user.id).aggregate(Sum('price_total'))
post.save()
cart_list = CartItem.objects.filter(user_id=request.user.id)
for cart in cart_list:
formOrder = AddOrderItemForm(request.POST)
post_cart = formOrder.save(commit=False)
post_cart.order = post
post_cart.product = cart.product
post_cart.composition = cart.composition
post_cart.price = cart.price
post_cart.quantity = cart.quantity
post_cart.total = cart.price_total
post_cart.price_discount = cart.price_discount
post_cart.price_reserved = cart.price_reserved
post_cart.save()
#cart_list.delete() #cancello carrello dopo ordine
return redirect('/order', pk=post.pk)
else:
form = AddOrderForm()
return render(request, 'order-form.html', {'form': form})
我的模型訂單,接收和值,和形式:
class Order(models.Model):
user = models.ForeignKey(User, null=True, blank=True, verbose_name="Utente")
code = models.CharField('Codice', max_length=250, null=True, blank=True)
tot_price = models.DecimalField('Prezzo', max_digits=10, decimal_places=2, blank=True, null=True)
tot_discount = models.DecimalField('Totale Scontato', max_digits=10, decimal_places=2, blank=True, null=True)
tot_price_reserved = models.DecimalField('Prezzo Scontato Riservato', max_digits=10, decimal_places=2, blank=True, null=True)
pub_date = models.DateTimeField('date published', editable=False)
inlavorazione = models.BooleanField('in lavorazione', default=False)
pagato = models.BooleanField('pagato', default=False)
spedito = models.BooleanField('spedito', default=False)
chiuso = models.BooleanField('chiuso', default=False)
def save(self, *args, **kwargs):
self.pub_date = datetime.now()
super(Order, self).save(*args, **kwargs) # Call the "real" save() method.
def __unicode__(self):
return self.pub_date.strftime('%Y-%m-%d')
class Meta:
verbose_name_plural = "Ordine"
ordering = ['id']
形式:
class AddOrderForm(ModelForm):
class Meta:
model = Order
fields = ['user', 'tot_price', 'tot_discount', 'tot_price_reserved']
您可以發佈您使用的模板和模型嗎? – vmonteco
看起來你想將字典轉換爲十進制?很難說沒有看到你正在使用的數據。 – ospahiu
我有更新的模型等.... –