Friday, May 27, 2011

Re: Getting "most"

And that works, with one
exception: .get_object_for_this_type(pk=object['object_id'])

Getting the objects IS a bit inefficient, but since I only need the
top 10, I can live with it.

On May 27, 12:32 pm, Jason Culverhouse <ja...@mischievous.org> wrote:
> If you were to do something like this:
>
> from django.models import count
> most = Watch.objects.values('content_type', 'object_id').annotate(Count('object_id')).order_by('-object_id__count')
>
> [
>         {'object_id__count': 15, 'object_id': 1, 'content_type': 10},
>         {'object_id__count': 2, 'object_id': 1, 'content_type': 5},
>         ...
> ]
>
> # this isn't efficient, but you get the idea
> # you need to resolve the content types and object id's into models
> from django.contrib.contenttypes.models import ContentType
> for object in most:
>         foo = ContentType.objects.get(id= object['content_type']).get_object_for_this_type(object['object_id'])
>
> You can also seehttps://github.com/coleifer/django-generic-aggregation
>
> This will get you close but I think it only if you want to annotate the count on a single content type at once.
>
> Jason Culverhousehttp://www.mischievous.org

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate