0

model.py

class Fatture(models.Model):
    numero = models.CharField(max_length=30, null=True, blank=True)
    data =  models.DateField()
    iva  =  models.PositiveIntegerField()
    commissione =  models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)

class Ddts(models.Model):
    fattura = models.ForeignKey('Fatture') 

class DdtsArticoli(models.Model): 
    ddt = models.ForeignKey('Ddts')
    articolo = models.ForeignKey('Articoli')
    quantita = models.DecimalField(max_digits=6, decimal_places=2)
    prezzo =  models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
    colli  =  models.PositiveIntegerField()

I have to make a query that calculate this total:

  1. aggregate table DdtsArticoli SUM(quantita * prezzo)

  2. aggregate table DdtsArticoli ((quantita * prezzo) / (1 + (iva of table Fatture)/100 ))

  3. resut of 1) - result of 2)
2
  • What do you group by? Or you want the total sum of the whole table? Commented Apr 29, 2015 at 17:57
  • Whole table not group by Commented Apr 29, 2015 at 18:01

1 Answer 1

1

See the Django docs on aggregation for detail but 1. should be something like:

from django.db.models import Sum

DdtsArticoli.objects.all().aggregate(
    your_key=Sum(F('quantita') * F('prezzo'), output_field=FloatField()))

Number 2. is no aggragation, you can simply calculate it:

(obj.quantita * obj.prezzo) / (1 + (obj.ddt.fattura.iva/100.0))

Where obj is your object aka. your database row.

Number 3. should be trivial then.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.