Wednesday, November 20, 2013

Re: Using Aggregate and Count

Hi Daniel,

On Wed, Nov 20, 2013 at 5:47 PM, Daniel Roseman <daniel@roseman.org.uk> wrote:
> On Wednesday, 20 November 2013 19:11:26 UTC, Timothy W. Cook wrote:

>
> You're accessing a `review_count` attribute via each Paper instance, but you
> haven't set up anything that would do that - you only have a single
> top-level `review_count` dictionary.
>
> Instead of a separate `aggregate` call, you should be using `annotate` on
> the original Paper queryset:
>
> papers =
> Paper.objects.filter(project__id=self.kwargs['pk']).annotate(review_count=Count('review'))
>
> Now each element in `papers` has a `review_count` attribute.

I had actually tried this. However, this is a reverse relationship
The foreignkey is on Review to Paper. So if I use your suggestion or
even:
papers = Paper.objects.filter(project__id=self.kwargs['pk']).annotate(review_count=Count('review__paper'))

Which I first thought was correct. I get a FieldError:
FieldError at /papers/papers_list/1
Cannot resolve keyword 'review' into field. Choices are: abstract, authors, ...

I thought that the docs at:
https://docs.djangoproject.com/en/1.6/topics/db/aggregation/#following-relationships-backwards

say this would work but apparently Django can't find the Review->Paper
relationship.

I'm stumped.

--Tim

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B%3DOU3WgyTOLKtPC1Rds3Sxmrwbs%3D3kDeV7t8UXJ0DY9XtWgJA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate