Django

django fliter 에다 list 넣기

이농이능 2018. 3. 29. 16:49
ids = [1, 3, 6, 7, 9]
for id in ids:
   MyModel.objects.filter( pk=id )

이런식으로 리스트의 값들만 filter 에 넣어서 가지고 오고 싶을때.


# Get blogs entries with id 1, 4 and 7
>>> Blog.objects.filter(pk__in=[1,4,7])

# Get all blog entries with id > 14
>>> Blog.objects.filter(pk__gt=14)

__in 을 통해서 가능하다.





출처 : 

https://stackoverflow.com/questions/9304908/django-filter-with-list-of-values?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa