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.
- Getting a list from Django into Javascript as an array
- Django and Shibboleth
- Django model: Email field unique if not null/blank
- Python Django custom template tags register.assignment_tag not working
- Django – http code 304, how to workaround in the testserver?
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)
- Django admin inline many to many custom fields
- Error: command 'x86_64-linux-gnu-gcc' when installing mysqlclient
- Django template tag: How to send next_page in {url auth_logout}?
- Django 1.7 removing Add button from inline form