You can also do that using the ORM instead of SQL, just instead of doing the calls on the instanced model use a manager and do the calls on all the objects in the database, something like this:
class Objectmanager(models.Manager): def SumAB(self): return sum([(obj.a + obj.b) for obj in MyObject.objects.all()]) class MyObject(models.Model): a = models.IntegerField() b = models.Integerfield() objects = ObjectManager() 2014-11-24 11:53 GMT+00:00 Jorge Andrés Vergara Ebratt <javebr...@gmail.com> : > Thanks, the best bet is the raw query, because I actually need the Sum of > the method result for all the instances in the query... > > 2014-11-24 3:52 GMT-05:00 monoBOT <monobot.s...@gmail.com>: > >> You can create a model method (with no parameter) that can solve that, so >> you can call it from the templates. >> >> something like: >> >> class MyObject(models.Model): >> a = models.IntegerField() >> b = models.Integerfield() >> >> def miOperation(self): >> return self.a + self.b >> >> def miOtherOpertion(self): >> return self.a * self.b >> >> >> >> 2014-11-24 4:21 GMT+00:00 Ethan Blackburn <ethan@ethan.coffee>: >> >>> related: >>> https://groups.google.com/forum/#!topic/django-users/s9qgXC4TNrA >>> >>> Short answer: you can't. A raw query would be your best bet(i.e. "SELECT >>> SUM(a + b) AS sum FROM app_X") >>> >>> >>> On Sunday, November 23, 2014 4:42:48 PM UTC-6, Jorge Andrés Vergara >>> Ebratt wrote: >>>> >>>> Hello everyone, >>>> >>>> Well, the tittle says it all: >>>> >>>> I have a model X >>>> >>>> class X(models.Model) >>>> a = models.IntegerField() >>>> b = models.IntegerField() >>>> >>>> def getC(self): >>>> return a + b >>>> >>>> >>>> So, when I'm inside a template I can call {{x.getC}} and it gets the >>>> method for the current instance it's in, but how can I get the Sum, or Avg >>>> of that method? I tried doing Sum('getC') but I get the error because getC >>>> isn't callable in the query... >>>> >>>> Thanks in advance for the help >>>> >>>> -- >>>> *Jorge Andres Vergara Ebratt* >>>> *#SoftwareDeveloper (Or at least trying to be)* >>>> *@javebratt* >>>> *facebook.com/javebratt <http://facebook.com/javebratt>* >>>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Django users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to django-users+unsubscr...@googlegroups.com. >>> To post to this group, send email to django-users@googlegroups.com. >>> Visit this group at http://groups.google.com/group/django-users. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/django-users/b5350447-264f-4145-be9d-8ea709673918%40googlegroups.com >>> <https://groups.google.com/d/msgid/django-users/b5350447-264f-4145-be9d-8ea709673918%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> >> >> -- >> *monoBOT* >> Visite mi sitio(Visit my site): monobotsoft.es/blog/ >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users+unsubscr...@googlegroups.com. >> To post to this group, send email to django-users@googlegroups.com. >> Visit this group at http://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/CA%2BxOsGBZFBmfcqJDMuRppms8CVp8yc0-nnWpymkomUUQ07_fbQ%40mail.gmail.com >> <https://groups.google.com/d/msgid/django-users/CA%2BxOsGBZFBmfcqJDMuRppms8CVp8yc0-nnWpymkomUUQ07_fbQ%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > *Jorge Andres Vergara Ebratt* > *#SoftwareDeveloper (Or at least trying to be)* > *@javebratt* > *facebook.com/javebratt <http://facebook.com/javebratt>* > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CAAeX05Fj7zX%2B_4fyW9d8c20rg6ggRxvm1PavmRA4ymrNTSDkbQ%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAAeX05Fj7zX%2B_4fyW9d8c20rg6ggRxvm1PavmRA4ymrNTSDkbQ%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- *monoBOT* Visite mi sitio(Visit my site): monobotsoft.es/blog/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BxOsGAv8g8rxNtnz1uBT6F4g_rjHjgb1taeDhUOUEfqoWEdmw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.