I am trying to filter Objects in django for a query set instead of a single value. Please see my code below
@api_view(['GET'])
def getOffersReceived(request, name):
owner = Profile.objects.get(name=name)
dogs = Dog.objects.filter(owner=owner)
print(dogs)
sittings = Sitting.objects.filter(dog=dogs)
return Response()
The print(dogs) line is showing a query set of 4 values. The next step I am trying to get all the sittings that have value dog matching either one of the items in the query set.
I am getting the following error:
ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.
any help would be greatly appreciated.
Thank you
CodePudding user response:
You can filter directly with:
@api_view(['GET'])
def getOffersReceived(request, name):
sittings = Sitting.objects.filter(dog__owner__name=name)
# …
return Response()
CodePudding user response:
You should use in
to filter the queryset:
https://docs.djangoproject.com/en/3.2/ref/models/querysets/#in
sittings = Sitting.objects.filter(dog__in=dogs)