简体   繁体   English

django-管理模型从模型中的另一个字段将值分配给limit_choices_to

[英]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.

相关问题 Django 模型限制基于另一个模型但具有特定字段值的选择 - Django model limit choices based on another model but with a specific field value 取决于该模型类中可选字段的ForeignKey中的limit_choices_to - limit_choices_to in a ForeignKey that depends on an optional field in that model class Django:添加ForeignKey limit_choices_关联另一个或相同的Model实例 - Django: add ForeignKey limit_choices_to associate another or same Model instance Django MTMField:limit_choices_to = other_ForeignKeyField_on_same_model? - Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model? 模型 limit_choices_to={'user': user} - Model limit_choices_to={'user': user} Django:使用 limit_choices_to 来限制管理菜单中的选择 - Django: Use limit_choices_to to limit choices in Admin menu 在模型中动态指定 limit_choices_to - Specify limit_choices_to in model dynamically Django 1.7管理:'ModelMultipleChoiceField'对象没有属性'limit_choices_to' - django 1.7 admin: 'ModelMultipleChoiceField' object has no attribute 'limit_choices_to' 限制管理员选择使用limit_choices_to - Limiting Admin Choices Using limit_choices_to 为引用当前模型的OneToOneField定义limit_choices_to - Defining a limit_choices_to for a OneToOneField which references the current model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM