Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 890
Imran Azad Avatar asked Jan 06 '12 18:01

Imran Azad


2 Answers

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!

like image 104
Ahsan Avatar answered Nov 05 '22 11:11

Ahsan


For Django 1.9

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

like image 11
Seb Avatar answered Nov 05 '22 09:11

Seb