Imran Azad
Imran Azad

Reputation: 1404

Django limit_choices_to on user group

I have the following model field:

name = models.ForeignKey(User, unique=False, editable=False, limit_choices_to=   
{'is_staff': False})

How can I limit the choices based on a specific group of users as opposed to limiting to specific users based on a flag. Is it possible to somehow limit choices based on auth_user_groups?

Thanks

Upvotes: 13

Views: 4510

Answers (3)

Seb
Seb

Reputation: 312

For Django 1.9

limit_choices_to={'groups__name': 'My Group'}

Upvotes: 12

Valentin Kantor
Valentin Kantor

Reputation: 1844

 limit_choices_to={'groups__pk': 2}

Upvotes: 0

Ahsan
Ahsan

Reputation: 11832

Yes, you can limit choices based on groups, here is one example

user = models.ForeignKey(User, unique=False, limit_choices_to= Q( groups__name = 'GroupName') )

try this, it works!

Upvotes: 14

Related Questions