Created
March 15, 2018 07:07
-
-
Save hwshim0810/3963397ed1b7e1fda37355659abd0b3b to your computer and use it in GitHub Desktop.
Django queryset filter : group by max limit 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
class SampleCategoryModel | |
id = primary | |
class SamplePostModel | |
category = foreignkey(SampleCategoryModel) | |
created_at = datetimefield | |
""" | |
from django.db.models import Max | |
query_set = SamplePostModel.objects.all() | |
condition = query_set.values('category').annotate(last_row_at=Max('created_at')) | |
query_set = query_set.filter(created_at__in=condition.values('last_row_at')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment