[Fixed]-Issues with queryset and slicing

18👍

Company is a queryset. You might want to do

Product.objects.filter(company=company[0], pk=product_pk)

Or better yet you can use the relations in the ORM to simplify into 1 lookup.

Product.objects.get(company__account=account, company__pk=company_pk, pk=product_pk) 

19👍

As said in the accepted answer, company is a queryset.

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

Instead of this

product = Product.objects.filter(company=company, pk=product_pk)

try this

product = Product.objects.filter(company__in=company, pk=product_pk)

__in can handle querysets larger than one (multiple records of a table).

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

Django documentation can be scary for a beginner like me because of its length and depth, though it provides solutions to most issues if you can crack it.

0👍

This error can also be created when you pass a queryset for the value you are searching with.

Company.objects.filter(account=account, pk=company_pk)

actually returns a queryset and that queryset can’t be used in this query

Product.objects.filter(company=company, pk=product_pk)

without producing the error message.

What is really missing here is a .get, either like

Company.objects.filter(account=account, pk=company_pk).get()

or

Company.objects.get(account=account, pk=company_pk)

Leave a comment