JD Yang
JD Yang

Reputation: 371

django filter : can filter with tuple?

Suppose, there is a list

strings = ['a','b','c']

And There are two model

class theModel:
   theString = models.charField()

Class superModel:
   hasClass = models.ForeignKey(theModel)

Is there any way to filter superModel by 'theString' with list?

for example this can be one way ( but is there any better way? without for loop )

tuple = []
for string in strings
   tuple.append ( theModel.objects.filter(theString = string) )

result = []
for theModel in tuple 
   result.append ( superModel.objects.filter(hasClass = theModel ) )

return result

Upvotes: 1

Views: 1148

Answers (1)

YardenST
YardenST

Reputation: 5266

You can do this:

theModel.objects.filter(theString__in=[1,4,7])

Upvotes: 3

Related Questions