2011-07-05 3 views
0

필자는 UserProfile 모델을 쿼리해야하지만 모든 사용자 레코드에 대해 auth.User 모델의 필드를 얻어야하는 경우가 있습니다.UserProfile에서 필드를 필터링 할 때 auth.User에서 사용자 이메일을받는 방법?

groups_list = User.objects.filter(status__in=group_list).values_list('email', flat=True) 

필터의 "status"는 UserProfile 모델에서 가져온 것입니다.

어느 누구도이 문제를 해결할 수 있습니까?

고맙습니다.

Erez

답변

1

, 감사합니다. 당신의 UserProfile 모델에서

groups_list = User.objects.filter(userprofile__status__in=group_list).values_list('email', flat=True) 
+0

이렇게 간단합니다. 이것은 때때로 "python/django"에서 잃는 "매직"의 일부입니다. 감사합니다 :-) – Erez

1

User에 외래 키가 related_name='profile' 세트가 있는지 확인하십시오 따라서 그것은 무언가 같이 수 있습니다. 그런 다음 다음을 수행 할 수 있습니다.

groups_list = User.objects.filter(profile__status__in=group_list).values_list('email', flat=True) 
+0

해답을 주셔서 감사합니다. 그러나 다른 하나는 훨씬 간단했습니다. 하지만 대단히 감사합니다 :-) – Erez

관련 문제