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.


On Monday, 06 August, 2018 09:35 PM, Matthew Pava wrote:

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’))

*From:*django-users@googlegroups.com [mailto:django-users@googlegroups.com] *On Behalf Of *Gerald Brown
*Sent:* Monday, August 6, 2018 8:29 AM
*To:* Django users
*Subject:* Django ORM aggregate Sum function on foreign key

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

--
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 <mailto:django-users+unsubscr...@googlegroups.com>. To post to this group, send email to django-users@googlegroups.com <mailto:django-users@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 <https://groups.google.com/d/msgid/django-users/c47e4bb7-626c-4b07-9ab5-55d68fe4d566%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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 <mailto:django-users+unsubscr...@googlegroups.com>. To post to this group, send email to django-users@googlegroups.com <mailto:django-users@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/3a17d4eeeb804cc89c4cda5eaca8b050%40ISS1.ISS.LOCAL <https://groups.google.com/d/msgid/django-users/3a17d4eeeb804cc89c4cda5eaca8b050%40ISS1.ISS.LOCAL?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/03e77e95-26b7-01e9-e11b-beca97d16176%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to