• Register
0 votes
3.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

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

1 vote
1 answer 53 views
53 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 Martin K 6.6k points
0 votes
2 answers 262 views
262 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 jwilliam 3.9k points
0 votes
2 answers 390 views
390 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 1.7K views
1.7K views
Problem : I want to deploy the Django application to a web, but I face the below error: “You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path”
asked Feb 3 jwilliam 3.9k points