[英]django - admin model assign value to limit_choices_to from another field inside the model
I have extended the admin model, so for each staff member i can assign other customers only if they are in the same group. 我扩展了管理模型,因此对于每位工作人员,我只能分配其他客户(如果他们属于同一组)。
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
is_manager = models.BooleanField(default=False)
group_account = models.CharField(max_length=3,blank=True,null=True)
clients_assigned = models.ManyToManyField(User, limit_choices_to = Q(groups__groupaccount__group_account=group_account),blank=True,null=True,related_name='+')
class UserProfileInline(admin.StackedInline):
model = UserProfile
verbose_name = "User extra"
verbose_name_plural = "extra"
filter_horizontal = ('clients_assigned', )
def save_model(self, request, obj, form, change):
return super().save_model(request, obj, form, change)
class UserAdmin(BaseUserAdmin):
inlines = [UserProfileInline, ]
def get_form(self, request, obj=None, **kwargs):
#permissions reduce for regular staff
if (not request.user.is_superuser):
self.exclude = ("app")
self.exclude = ("user_permissions")
## Dynamically overriding
#self.fieldsets[2][1]["fields"] = ('is_active', 'is_staff','is_superuser','groups')
self.fieldsets[2][1]["fields"] = ('is_active', 'is_staff')
form = super(UserAdmin,self).get_form(request, obj, **kwargs)
return form
and extended the group admin model 并扩展了组管理员模型
class GroupAccount(models.Model):
group_account = models.CharField(,max_length=3,blank=True,null=True)
group = models.OneToOneField(Group,blank=True,null=True)
def save(self, *args, **kwargs):
super(GroupAccount, self).save(*args, **kwargs)
what im trying to do is simply to limit the client list for each manager-user by his and their group indicator(group_account field), means the available client list are those whom have the same specific group as himself, such as '555' 我试图做的只是通过每个经理用户及其组指示器(group_account字段)限制每个客户的客户列表,这意味着可用的客户列表是与他自己具有相同特定组的客户列表,例如“ 555”
when the group_account = '555' in the DB the result of groups__groupaccount__group_account=group_account are empty 当数据库中的group_account ='555'时,groups__groupaccount__group_account = group_account的结果为空
but if i change it Hardcoded to: groups__groupaccount__group_account='555' its return the relevant result. 但是如果我将其硬编码更改为:groups__groupaccount__group_account ='555',则会返回相关结果。
is that possible and/or what the alternative? 那有可能和/或其他选择吗?
django 1.9 Django 1.9
thanks for the help 谢谢您的帮助
You should custom the formfield_for_foreignkey method of StackInline 您应该自定义StackInline的formfield_for_foreignkey方法
class UserProfileInline(admin.StackedInline):
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
field = super(UserProfileInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
if db_field.name == 'clients_assigned':
u = request.user
if not u.is_superuser:
field.queryset = field.queryset.filter(groups__in=u.groups.all())
return field
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.