employees = Employee.objects.filter(age=23, sex='female')
This would return a queryset.
If I assume this queryset only contains one result, how to return that object directly?
Is there any way like using 'get'?
Actually, you can pass multiple lookup parameters in QuerySet's get() method. So how about?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With