employees = Employee.objects.filter(age=23, sex='female')
这将返回一个查询集。
如果我假设这个查询集只包含一个结果,那么如何直接返回该对象?
有没有像使用'get'的方法?
答案 0 :(得分:38)
实际上,您可以在QuerySet的get() method中传递多个查找参数。那怎么样?
try:
employee = Employee.objects.get(age=23, sex='female')
except Employee.DoesNotExist:
# no employee found
except Employee.MultipleObjectsReturned:
# what to do if multiple employees have been returned?