Tuesday, August 14, 2018

Re: GROUP BY

@galan:
market__market is foreign key relation to the human readable value for market, rather than it's id
datePipeline__year is the year of the datefield column called datePipeline
Sorry for not posting the models, that would have avoided the confusion.
But thanks anyways :)

@Matthew:

Yes, you're right. Dictionary solved part of the problem. The following formulation (not grouping by week number as is the final intent) works:

    contract_query = ( Project.objects
                             
.values('market__market')
                             
.filter(department=department_id)
                             
.filter(datePipeline__year=year)
                             
.filter(stage="WON")
                             
.annotate(contracted=Sum('ownProduction'))
                             
.annotate(week=ExtractWeek('dateContract'))


So, trying to include the week number in my group by is


tirsdag den 14. august 2018 kl. 15.28.01 UTC+2 skrev Matthew Pava:

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: django...@googlegroups.com [mailto:django...@googlegroups.com] 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: django...@googlegroups.com [mailto:django...@googlegroups.com] 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 django-users...@googlegroups.com.
To post to this group, send email to djang...@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/525b78b7-ada3-4a49-818a-c02fc1bf51b0%40googlegroups.com.
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...@googlegroups.com.
To post to this group, send email to djang...@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/d2748446-4d61-44a4-b435-c77eb4957818%40googlegroups.com.
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+unsubscribe@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/634ae02f-920d-43c2-a5a7-ccae73a1e8a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment