• Register
0 votes
4.5k views

Problems :

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

class MyProduct(Meta):
    mycompany = models.ForeignKey(
        MyCompany, 
        related_name='myproducts', 
        on_delete=models.CASCADE
    )

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.
6 5 3
7,540 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.

9 7 4
38,600 points
its work!  thank you
thanks yaar for solving my doubt

Related questions

0 votes
1 answer 9 views
9 views
Problems : how can I fix this situation:The queryset value for an exact lookup must be limited to one result using slicing..
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 8 views
8 views
Problem: I work on a small Django app and get an error tell me: Super(type, obj): obj must be an instance or subtype of type Can someone explain the reason for the error to me and how to solve it?
asked Mar 22 ummesalma 22.8k points
1 vote
1 answer 1 view
1 view
Problem: I am getting this error.If some expert can put light on it , will be much appreciated. The type t must be a reference type.
asked Mar 25 PkGuy 13.1k points
1 vote
1 answer 130 views
130 views
Problem: I have fundamental knowledge of Django. I have learned Django with the help of online tutorials. Recently I have started working on Django. I have recently tried to upgrade my Django to the Django 1.10. Since then I am often facing below error: TypeError: ... ), ] I have only fundamental knowledge about Django so unable to fix the above error. Kindly help me in fixing above Django error.
asked Jun 8, 2020 Martin K 6.6k points
0 votes
2 answers 649 views
649 views
Problem : After upgrading it to Django 1.10, I get below error: “TypeError: view must be a callable or a list/tuple in the case of include().” My urls.py is as below : from django.conf.urls import include, url urlpatterns = [     url(r'^$', 'myapp.views.home' ... r'^contact/$', 'myapp.views.contact'),     url(r'^login/$', 'django.contrib.auth.views.login'), ] How can I fix above error?
asked Jan 23, 2020 jwilliam 3.9k points
0 votes
2 answers 575 views
575 views
Problem : I want to set up the multiple setting files (e.g. Dev, Prod, ..) that include some base settings also. But unable to gain successes in my goal. Whenever I am trying to run my ./manage.py runserver I ... == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cb.settings.development") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
asked Dec 5, 2019 alecxe 7.5k points
0 votes
1 answer 43 views
0 votes
1 answer 307 views
307 views
Problem : When I try to run following command: python manage.py updatestats I get the error as below: “TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'” I am unable to figure out ... ) File "/Users/alecxe/project/Dev/project_files/project/main/management/commands/updatestats.py", line 34, in handle unique_visits=display_unique_visits(hostname),)
asked Jan 2, 2020 alecxe 7.5k points
0 votes
1 answer 2.5K views
2.5K views
Problem : I am facing the issue in passing a function as a parameter to another function as follows typeerror: int() argument must be a string, a bytes-like object or a number, not 'nonetype'
asked Nov 18, 2019 peterlaw 6.9k points
0 votes
1 answer 12 views
12 views
Problem: Is it normal to be happen ? how can I fix it : You're using the staticfiles app without having set the static_root setting to a filesystem path..
asked Mar 6 Wafa Abu Yousef 6.1k points