Django ORM aggregate Sum function on foreign key

555 views
Skip to first unread message

Gerald Brown

unread,
Aug 6, 2018, 9:29:28 AM8/6/18
to Django users
Greetings:

I am having a problem using the above function.

In my table I have a field payment that is a foreign key to a payment table.

When I try to use aggregate(sum('payment')) it gives me the sum of the foreign key, not the value from the payment table.
Currently I have 9 records in my test DB each one has a foreign key of 3 so I get a sum of 27 not 2,250 that I should be getting.

This table also has a foreign key to the customer table and it returns the customer not the foreign key number.

The thought I had was to make that foreign key an integer field and then the user would have to enter a number instead of selecting it from a list, prone to operator errors,

Any ideas on how I can get the correct totals using ORM.

Thanks


Matthew Pava

unread,
Aug 6, 2018, 9:37:03 AM8/6/18
to django...@googlegroups.com

You need to specify the field of the value in your ORM statement.  I don’t know what your field name is, but this assumes that it is “value”:

aggregate(Sum(‘payment__value’))

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c47e4bb7-626c-4b07-9ab5-55d68fe4d566%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gerald Brown

unread,
Aug 6, 2018, 10:08:46 AM8/6/18
to django...@googlegroups.com

This is how I finally solved my problem!!!

The following is a copy of my ORM statement:

 Visit.objects.filter(visit_date = mydate).aggregate(total = Sum('visit_payment'))
{'total': Decimal('27')}
 This total is the total of the foreign key ids.  9 records each with a key of 3 = 9 x 3 = 27

The way I solved this problem is to put another variable in the loop where I was printing the records to Reportlab PDF file and I incremented this variable with the value that was being printed.  What was being printed was the value from the look-up table so I was able to get a Sum from that.

Here is the code of how I did it: vi = Visit.objects.filter(visit_date = mydate);  vip = 0; for v in vi:; vi_pay = str(v.visit_payment); vip += int(vi_pay)

Here is how the Reportlab is built: Also in the same loop as above I have the lines:  vi_pat = str(v.patient); pa1.drawString(x, y, str(vi_pat)); pa1.drawString(x + 175, y, str(vi_pay))

Each of these are on a separate line in my code.

Hopefully this will help anyone with similar problems.

Kidwell Nyatsambo

unread,
Aug 7, 2018, 2:32:51 PM8/7/18
to django...@googlegroups.com
Hello,
Which application / program uses vi. I would like to create a schema.sql to use with Python.

KN

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
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+unsubscribe@googlegroups.com.

--
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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Gerald Brown

unread,
Aug 7, 2018, 8:51:06 PM8/7/18
to django...@googlegroups.com

vi is just a variable that I created in my code.  It is just short for visit.  It has NOTHING to do with the vi editor program.  My editor is Geany.

I am using the Django ORM to query my database.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages