반응형
'has_location'과 'locations'라는 테이블이 있습니다. 'has_location'에는 user_has
및 location_id
와 django 자체에서 제공하는 자체 id
가 있습니다.
'위치'에 더 많은 열이 있습니다.
이제 특정 사용자의 모든 위치를 가져오고 싶습니다. 내가 한 일은 .. (user.id가 알려져 있음) :
users_locations_id = has_location.objects.filter(user_has__exact=user.id)
locations = Location.objects.filter(id__in=users_locations_id)
print len(locations)
하지만이 인쇄물
에 의해 0
이 나옵니다. DB에 데이터가 있습니다. 하지만 __ in
이 모델 ID를 받아들이지 않는다는 느낌이 듭니다.
감사
해결 방법
has_location의 고유 ID를 사용하여 위치를 필터링하고 있습니다. 위치를 필터링하려면 location_id
를 사용해야합니다.
user_haslocations = has_location.objects.filter(user_has=user)
locations = Location.objects.filter(id__in=user_haslocations.values('location_id'))
역관계를 통해 직접 위치를 필터링 할 수도 있습니다.
location = Location.objects.filter(has_location__user_has=user.id)
참조 페이지 https://stackoverflow.com/questions/15636527
반응형
'파이썬' 카테고리의 다른 글
파이썬 Python에서 타이머 틱 가져 오기 (0) | 2021.01.23 |
---|---|
파이썬 Postgres의 URI에 연결 (0) | 2021.01.23 |
파이썬 sqlalchemy.exc.ArgumentError : 플러그인을로드 할 수 없음 : sqlalchemy.dialects : driver (0) | 2021.01.23 |
파이썬 Python 3.x : 다음 줄로 이동 (0) | 2021.01.22 |
파이썬 Python에 pygraphviz가 표시되지 않음 (0) | 2021.01.22 |
댓글