Cannot convert {'price_total__sum': Decimal('258.00')} to Decimal
<p>I get this error when I do a Sum of an entire column:</p>

<pre><code>Cannot convert {'price_total__sum': Decimal('258.00')} to Decimal
</code></pre>

<p>here the entire project: <a href="https://github.com/pierangelo1982/djangocommerce/tree/berge" rel="nofollow">https://github.com/pierangelo1982/djangocommerce/tree/berge</a></p>

<pre><code>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})
</code></pre>

<p>My Models Order, that receive the sum value, and the form:</p>

<pre><code>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']
</code></pre>

<p>Form:</p>

<pre><code>class AddOrderForm(ModelForm):
    class Meta:
        model = Order
        fields = ['user', 'tot_price', 'tot_discount', 'tot_price_reserved']
</code></pre>

<p>As the error shows, your <code>aggregate()</code> call returns a dictionary: <code>{'price_total__sum': Decimal('258.00')}</code>. You can't set that dict as a DecimalField in the target model; you need to extract the actual value first.</p>

<pre><code>price_total = CartItem.objects.filter(user_id=request.user.id).aggregate(Sum('price_total'))
post.tot_price = price_total['price_total__sum']
</code></pre>

python django virtualenv