views:

68

answers:

2
      idarr = [1,2,3,4,5]
      for i in  range(len(idarr)):
          upload.objects.filter(idarr[i])

Cant we pass the idarr at one shot to the query

+7  A: 

I am assuming that you are trying to filter all instances of Upload whose id is in the list idarr. If that is the case then you can go about it like this:

Upload.objects.filter(id__in = idarr)

Read the documentation for more details.

Manoj Govindan
@simplyharsh: thanks for adding the missing verb.
Manoj Govindan
+7  A: 

So much wrong in so few lines...

  1. In Python, never loop through range(len(whatever)). Just do for i in whatever.

  2. Assuming upload is a Django model, you can't just pass a value to filter - you need to say what you're filtering against. Presumably it's the primary key, so you want .filter(pk=i).

  3. If you want to filter against any of the values in a list, use __in: .filter(pk__in=idarr).

Daniel Roseman