Home > Net >  Django - Using a complex query
Django - Using a complex query

Time:10-05

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.

CodePudding user response:

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

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