django - How to filter a query using the max(date) value -
i have model:
class logbook_audit(models.model): id = models.autofield(primary_key=true) logbook = models.foreignkey(logbook, verbose_name='logbook control no.') username = models.foreignkey(user, null=true, blank=true, verbose_name='username') status = models.charfield(null=false, blank=false, max_length=40, choices=status_type, default=u'checking requirements', verbose_name='status') log_in = models.datetimefield(default=lambda:datetime.now(), verbose_name='work start')
i'm trying create queryset find latest user log_in such specific logbook , status.
view.py
undolink(request, pk): undo_rec = logbook.objects.get(pk=pk) latest_user = logbook_audit.objects.filter(logbook_id=pk, status=undo_rec.status).aggregate(max('log_in')) ...
but latest_user giving me latest log_in format. thinking can used latest_user.username
failed. there way find username? guide appreciated.
you can use latest()
instead:
latest_log = logbook_audit.objects.filter(logbook_id=pk, status=undo_rec.status).latest('log_in')
then can access user through latest_log.user
.
Comments
Post a Comment