Skip to content

Instantly share code, notes, and snippets.

@nkryptic
Last active December 14, 2015 02:39
Show Gist options
  • Save nkryptic/5015694 to your computer and use it in GitHub Desktop.
Save nkryptic/5015694 to your computer and use it in GitHub Desktop.
import operator
from django.db import models
from django.contrib.auth.models import User
from myapp.models import License
or_list = []
values = User.objects.values_list('id').annotate(models.Max('licenses__created_at'))
for pk, dt in values:
or_list.append(models.Q(user__pk=pk, created_at=dt))
qs = License.objects.filter(reduce(operator.or_, or_list))
for license in qs:
print license.id, license.created_at, license.user.username
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment