Jeff Sinason
Jeff Sinason

Reputation: 81

Django - Using a complex query

I've read almost all of the related questions and still can't figure out how to execute the following query in Django.

Using the Django standard tables for Auth I've added a group called 'approvers'. I need to query to return all approvers. In SQLite designer I developed the following sql:

select auth_user.email, auth_user.first_name, auth_user.last_name 
 from
     auth_user, auth_user_groups 
 where 
     auth_user.id = auth_user_groups.user_id
 and 
     auth_user_groups.group_id in 
          ( select auth_group.id from auth_group where auth_group.name = "approvers")

It seems that I should be able to do this by using the raw method on the models, but would like to understand how to use the Django ORM to access this if it's a better more acceptable approach.

Upvotes: 1

Views: 68

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477607

You can .filter(…) [Django-doc] with:

User.objects.filter(groups__name='approvers')

Upvotes: 1

Related Questions