django queryset return single value
Assuming owner_name
is unique, either of these will do the trick:
owner_id = Owner.objects.only('owner_id').get(owner_name=owner_name).owner_id
owner_id = Owner.objects.values('owner_id').get(owner_name=owner_name)['owner_id']
owner_id = Owner.objects.values_list('owner_id', flat=True).get(owner_name=owner_name)
Documentation:
only()
values()
values_list()
For posterity, this also works and is (in my opinion) the cleanest option of all:
Owner.objects.values_list('owner_id', flat=True).get(owner_name=owner_obj)