2

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

2 Answers 2

2

You can filter directly with:

@api_view(['GET'])
def getOffersReceived(request, name):
    sittings = Sitting.objects.filter(dog__owner__name=name)
    # …
    return Response()
Sign up to request clarification or add additional context in comments.

Comments

2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.