On 20 août, 00:32, widoyo <wid...@gmail.com> wrote:
> Try to add function 'quota_ore' below.
>
> On Aug 19, 3:03 am, djnubbio <giuseppe.nati...@gmail.com> wrote:> Sorry for 
> wasting your preciuose time. I'm very newby in django.
>
> > I have de following
>
> > class Commessa(models.Model):
> >     commessa = models.CharField(max_length=20)
> >      quota_oraria=models.DecimalField(max_digits=5, decimal_places=2)
> >   ...
>
>    def quota_ore(self):
>       self.quota_oraria * sum([d.ore for d in self.diario_set.all()])

Actually,

    def quota_ore(self):
       return self.quota_oraria * sum([d.ore for d in
self.diario_set.all()])

would work better... At least it would return something instead of
None !-)

Then you could avoid useless model instances creation, as well as the
temporary list:

    def quota_ore(self):
       return self.quota_oraria *
sum(self.diario_set.values_list('ore', flat=True))


But still : sum() is a standard SQL aggregation function, so it
probably (yes, that's an understatement) would be faster and cheaper
to just let the SQL engine do the hard work:


from django.db.models import Sum

     def quota_ora(self):
       return self.quota_oraria * self.diario_set.aggregate(Sum('ore'))
['sum__ore']


NB : not tested but according to the FineManual and my it should
JustWork(tm).
HTH

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to