Often I find myself wanting to get the first object from a queryset in Django, or return None
if there aren\'t any. There are lots of ways to do this which all
It can be like this
obj = model.objects.filter(id=emp_id)[0]
or
obj = model.objects.latest('id')
Use the convenience methods .first() and .last():
MyModel.objects.filter(blah=blah).first()
They both swallow the resulting exception and return None
if the queryset returns no objects.
These were added in Django 1.6, which was released in Nov 2013.