sql query to annotation values e aggretate statements

16 views
Skip to first unread message

Mauro Ziliani

unread,
Jan 29, 2015, 9:37:34 AM1/29/15
to django...@googlegroups.com
Hi all.
My name's Mauro and I start to work with Django 1.7.3
I work on my project which tracks my accounting.

I need to translate a SQL query to annoteta values aggretate statements.

The application is named accounting

The models involved are:


class Nominativo(models.Model):
  nome = models.CharField(max_length=100)
  
class Impianto(models.Model):
  nome = models.CharField(max_length=100)

class Capitolo(models.Model):
  nome = models.CharField(max_length=100)
  

class Registrazione(models.Model):
  anno = models.PositiveIntegerField()
  impianto = models.ForeignKey('Impianto')
  nominativo = models.ForeignKey('Nominativo')
  capitolo = models.ForeignKey('Capitolo')

  dare = models.DecimalField(max_digits=10, decimal_places=2)
  avere = models.DecimalField(max_digita=10, decimal_places=2)


I need to translate te following sql statement

SELECT capitolo.nome, sum(dare) as uscite, sum(avere) as entrate 
FROM contabilita_registrazione 
INNER JOIN contabilita_capitolo as capitolo ON contabilita_registrazione.capitolo_id=capitolo.id 
WHERE anno=2014 
GROUP BY capitolo.nome 
ORDER BYcapitolo.nome;

How can obtain the same behavior using ORM?

Thanks in advance

MZ
Reply all
Reply to author
Forward
0 new messages