• Register
0 votes

Problems :

I have 2 models, MyCompany and MyProduct, with MyProduct having the ForeignKey to MyCompany

class MyProduct(Meta):
    mycompany = models.ForeignKey(

I have done the below filtering :

mycompany = MyCompany.objects.filter(myaccount=myaccount, pk=company_pk)
        if not mycompany:
            raise Http404
myproduct = MyProduct.objects.filter(mycompany=mycompany, pk=product_pk)
        if not myproduct:
            raise Http404
        return myproduct

And I am facing below error:

The QuerySet value for an exact lookup must be limited to one result using slicing.
7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I have gone through your question and I understood it.

I had faced the similar issue in the past and so know the solution on it.

Please note that the mycompany is the queryset.

The QuerySet value for the exact lookup must be limited to one result using slicing.

Instead of this please try to follow below approach:

myproduct = MyProduct.objects.filter(mycompany=mycompany, pk=product_pk)

try below correct line of code :

myproduct = MyProduct.objects.filter(mycompany__in=mycompany, pk=product_pk)

__in can handle the querysets larger than the one with multiple records of a table.

This can be found in your django Many-to_one relationships section of given documentation. https://docs.djangoproject.com/en/2.0/topics/db/examples/many_to_one/

The Django documentation can be very scary for the beginners like you because of its length and the depth, though it provides solutions to most the issues if you can correctly understand it.

38.6k points
its work!  thank you
thanks yaar for solving my doubt