Tuesday, May 14, 2013

Re: Forcing group_by on a field other than id


I assume you want to count the number of records based on the date; or in other words, number of times a song is played grouped by date..right?

If yes, does something like this help: 
Item.objects.extra({'created':"date(created_date)"}).values('created').annotate(created_count=Count('id')).order_by()



On Wed, May 15, 2013 at 7:09 AM, brian <bmillham@gmail.com> wrote:
I'm trying to do a query that needs a group_by on a field other than id.

I've tried this:

s=Song.objects.using('bmillham').annotate(Count('played__date_played')).order_by('-played__date_played')

the generated query is:

>>> print s.query
SELECT `song`.`id`, `song`.`file`, `song`.`catalog`, `song`.`album`,
`song`.`album`, `song`.`year`, `song`.`artist`, `song`.`artist`,
`song`.`title`, `song`.`bitrate`, `song`.`rate`, `song`.`mode`,
`song`.`size`, `song`.`time`, `song`.`track`, `song`.`update_time`,
`song`.`addition_time`, COUNT(`played`.`date_played`) AS
`played__date_played__count` FROM `song` LEFT OUTER JOIN `played` ON
(`song`.`id` = `played`.`track_id`) GROUP BY `song`.`id` ORDER BY
`played`.`date_played` DESC

I tried adding

s.query.group_by = [('played', 'date_played')]

but that doesn't change the query.

Am I missing something here, or can't this be done?

Thanks!
Brian

--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate