views:

160

answers:

2

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 :)

+4  A: 

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))
Javier
fragmentID__in(...) works, thx :)
Rin
+4  A: 

As others have said, we really need the models, and some explanation of what you're actually trying to achieve.

But it looks like you want to do a related table lookup. Rather than getting all the related objects in a separate nested query, you should use Django's related model syntax to do the join within your query.

Something like:

acceptedFragment.objects.filter(fragment__categories__id = 1)
Daniel Roseman
Wow i don't know that cool syntax, thx ;>
Rin