Ah, the error has the answer.  contract_row is a dictionary, so you need to 
access it as if it were a dictionary.
contract_sum = contract_row['contracted']

From: [email protected] [mailto:[email protected]] On 
Behalf Of Mikkel Kromann
Sent: Monday, August 13, 2018 11:51 PM
To: Django users
Subject: Re: GROUP BY


Thanks. But strange - exactly same error:

'dict' object has no attribute 'contracted'

    contract_query = (Project.objects.all()
                                 .filter(department=department_id)
                                 .filter(datePipeline__year=year)
                                 .filter(stage="WON")
                                 .annotate(week=ExtractWeek('dateContract'))
                                 .annotate(contracted=Sum('ownProduction'))
                                 .values('week','market__name', 'contracted')
                                )
    for contract_row in contract_query:
        contract_sum = contract_row.contracted

I am somewhat confused by this ...

Mikkel


mandag den 13. august 2018 kl. 22.24.48 UTC+2 skrev Matthew Pava:
You need to include “ownProduction” in your values list prior to the annotation.


From: [email protected]<javascript:> 
[mailto:[email protected]<javascript:>] On Behalf Of Mikkel Kromann
Sent: Monday, August 13, 2018 3:22 PM
To: Django users
Subject: GROUP BY

I'm trying to sum the field "ownProduction", grouping it by the foreign key 
"market" (using the market's human readable name, __name from the Market table) 
and the week number (which is built-in function __year of the date field 
"dateContract").
However, when I try to read the results of the query in the last line in the 
code example below., I get the error:

"'dict' object has no attribute 'ownProduction'"

So "ownProduction" is to my surprise not a field in the query.
What am I doing wrong?

    contract_query = (Project.objects.all()
                             .filter(department=department_id)
                             .filter(datePipeline__year=year)
                             .filter(stage="WON")
                             .annotate(week=ExtractWeek('dateContract'))
                             .values('week','market__name')
                             .annotate(Sum('ownProduction'))
                         )
    for contract_row in contract_query:
        contract_sum = contract_row.ownProduction

It doesn't change anything using
                             .annotate(ownProduction = Sum('ownProduction'))


cheers + thanks, Mikkel
--
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 [email protected]<javascript:>.
To post to this group, send email to [email protected]<javascript:>.
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/525b78b7-ada3-4a49-818a-c02fc1bf51b0%40googlegroups.com<https://groups.google.com/d/msgid/django-users/525b78b7-ada3-4a49-818a-c02fc1bf51b0%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 
[email protected]<mailto:[email protected]>.
To post to this group, send email to 
[email protected]<mailto:[email protected]>.
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/d2748446-4d61-44a4-b435-c77eb4957818%40googlegroups.com<https://groups.google.com/d/msgid/django-users/d2748446-4d61-44a4-b435-c77eb4957818%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 [email protected].
To post to this group, send email to [email protected].
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/bd075ec8f0114f3ba73ec356c3a575c1%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to