1
      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

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.