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

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -