Python – Really long query

djangodjango-modelspython

How do u do long query? Is there way to optimize it?
I would do complicated and long query:

all_accepted_parts = acceptedFragment.objects.filter(fragmentID = fragment.objects.filter(categories = fragmentCategory.objects.filter(id=1)))

but it doesn't work, i get:

Error binding parameter 0 - probably unsupported type.

I will be thankful for any hint how i could optimize it or solve of course too – more thankful 🙂

Best Answer

If it's not working, you can't optimize it. First make it work.

At first glance, it seems that you have really mixed concepts about fields, relationships and equality/membership. First go thought the docs, and build your query piece by piece on the python shell (likely from the inside out).

Just a shot in the dark:

all_accepted_parts = acceptedFragment.objects.filter(fragment__in = fragment.objects.filter(categories = fragmentCategory.objects.get(id=1)))

or maybe:

all_accepted_parts = acceptedFragment.objects.filter(fragment__in = fragment.objects.filter(categories = 1))