用户组上的Django limit_choices_to

时间:2021-07-12 19:38:00

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?

如何根据特定用户组限制选择,而不是基于标志限制特定用户。是否有可能以某种方式限制基于auth_user_groups的选择?

Thanks

谢谢

3 个解决方案

#1


10  

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!

试试这个,它有效!

#2


5  

For Django 1.9

对于Django 1.9

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

limit_choices_to = {'groups__name':'我的小组'}

#3


0  

 limit_choices_to={'groups__pk': 2}

#1


10  

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!

试试这个,它有效!

#2


5  

For Django 1.9

对于Django 1.9

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

limit_choices_to = {'groups__name':'我的小组'}

#3


0  

 limit_choices_to={'groups__pk': 2}