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
.annotate(ownProduction = Sum('ownProduction'))
You need to include “ownProduction” in your values list prior to the annotation.
--
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/525b78b7-ada3-4a49-818a-c02fc1bf51b0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'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
To post to this group, send email to djang...@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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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']
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/d2748446-4d61-44a4-b435-c77eb4957818%40googlegroups.com.
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'))
contract_query = ( Project.objects
.filter(department=department_id)
.filter(datePipeline__year=year)
.filter(stage="WON")
.annotate(contracted=Sum('ownProduction'))
.annotate(week=ExtractWeek('dateContract'))
.values('market__market','week')
contract_query = ( Project.objects
.filter(department=department_id)
.filter(datePipeline__year=year)
.filter(stage="WON")
.annotate(contracted=Sum('ownProduction'))
.annotate(week=ExtractWeek('dateContract'))
.values('market__market')
Sometimes it helps to print out the results one function at a time to get an idea what Django is returning.
If you want to include “contracted” in your result set, you have to include “contracted” in your values function. Values limits the result set to the columns you specify.
I suggest reviewing the annotation and values docs here:
https://docs.djangoproject.com/en/2.1/topics/db/aggregation/#order-of-annotate-and-values-clauses
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/410dbb7f-5b6f-4347-a637-5eaf4156f388%40googlegroups.com.
contract_query = ( Project.objects
.filter(department=department_id)
.filter(datePipeline__year=year)
.filter(stage="WON")
.annotate(contracted=Sum('ownProduction'))
.annotate(week=ExtractWeek('dateContract'))
.values('market__market','week','contracted')
)