Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

Django query to return distinct list + count + newest entry

I am having trouble writing a Django query that will return a list of markets from the Post table, as well as a post_count for that specific market, and the datetime of the most recent post.

Query:

Post.objects.values('market').annotate(post_count=Count('pk')).annotate(recent_post=Max('id')).order_by('market')

Results:

<QuerySet [{'market': 29, 'post_count': 2, 'recent_post': 6}, {'market': 150, 'post_count': 1, 'recent_post': 5}]>

This is relatively close, but I would ultimately like to return the market and recent post as an object, instead of an id.

Any help would be amazing! TIA

question from:https://stackoverflow.com/questions/65911492/django-query-to-return-distinct-list-count-newest-entry

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...