KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I need to fill in a template with a summary of user activity in a simple messaging system. For each message sender, I want the number of messages sent and the number of distinct recipients. Here's a simplified version of the model: class Message(models.Model): sender = models.ForeignKey(User, related_name='messages_from') recipient = models.ForeignKey(User, related_name='messages_to') timestamp = models.DateTimeField(auto_now_add=True) Here's how I'd do it in SQL: SELECT sender_id, COUNT(id), COUNT(DISTINCT recipient_id) FROM myapp_messages GROUP BY sender_id; I've been reading through the documentation on aggregation in ORM queries, and although annotate() can handle the first COUNT column, I don't see a way to get the COUNT(DISTINCT) result (even extra(select={}) hasn't been working, although it seems like it should). Can this be translated into a Django ORM query or should I just stick with raw SQL?
Tags (comma-separated)
Save Edits
Cancel