reduce, operator ; Python

운명애·2021년 1월 24일
0

textures = ['soft', 'gentle', 'oil']

query = reduce(operator.or_, ( (Q(name__contains = texture ) for texture in textures ))

위 코드는 아래와 같다.

query = Q(name__contains='soft') | Q(name__contains='gentle') | Q(name__contains='oil')

장고에서 검색 view 를 만들 때 이용하면 된다.

QueryDict 인 request.GET 검색요청이 왔을 때 특정 필드의 여러 값들이 동시에 들어가거나 값들 중 하나라도 들어가는 검색을 구현할 때 이용할 수 있다.

여러 값이 동시에 들어가는 경우는 operator.and_ 를 이용하면 된다.

profile
개발자 하고 싶어요..

0개의 댓글

관련 채용 정보