具有过滤查询集的组权限自定义表单

发布于 2024-09-12 19:55:21 字数 608 浏览 6 评论 0原文

我需要提供一个用户可以管理与某个组关联的权限的方法。

我想使用来自 django 的 forms.ModelForm 功能,但我无法理解如何修改字段循环的查询集。我还深入研究了 contrib.admin 和 contrib.auth 以发现这些表单是在哪里生成的,但找不到它。

我尝试不使用普通的 modelForm,因此已经设置了先例设置。

这就是场景。我已经向我的项目添加了 40 个(或多或少)权限,所有这些权限的代号都以“xxxxx”开头,所以我想这样做:

class PermissionGroup(forms.ModelForm):
#permissions = forms.ModelMultipleChoiceField(queryset = Permission.objects.filter(codename__startswith = 'xxxxx.'), widget=forms.CheckboxSelectMultiple) 
class Meta:
    model = Group
    fields = ('permissions',)

我怎样才能实现结果?或者如何将先例权限绑定到普通 form.ModelForm?

提前致谢!

I need to offer a from in which a user can manage the permission associated to some Group.

I'd like to use the forms.ModelForm feature which comes from django, but I cannot understand how to modify the queryset over which the field cycles. I've also taken a deep look in contrib.admin and contrib.auth to discover where those forms are generated but cannot find it.

I'm trying not to use a normal modelForm so the precedent settings are already setted.

This is the scenario. I've added 40 (more or less) permissions to my project, all them codename starts with 'xxxxx.', so I'd like to do this:

class PermissionGroup(forms.ModelForm):
#permissions = forms.ModelMultipleChoiceField(queryset = Permission.objects.filter(codename__startswith = 'xxxxx.'), widget=forms.CheckboxSelectMultiple) 
class Meta:
    model = Group
    fields = ('permissions',)

How can I achieve the result? Or how can I bind precedent permission to a normal form.ModelForm?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的他你的她 2024-09-19 19:55:21
class PermissionGroup(forms.ModelForm):
    permissions = forms.ModelMultipleChoiceField(Permission.objects.none(), widget=forms.CheckboxSelectMultiple)

    def __init__( self, pass_a_Q_object=None, *args, **kwargs ):
        super( PermissionGroup, self ).__init__( *args, **kwargs )
        if pass_a_Q_object:
            self.fields['permissions'].queryset = Permission.objects.filter(pass_a_Q_object) 
class PermissionGroup(forms.ModelForm):
    permissions = forms.ModelMultipleChoiceField(Permission.objects.none(), widget=forms.CheckboxSelectMultiple)

    def __init__( self, pass_a_Q_object=None, *args, **kwargs ):
        super( PermissionGroup, self ).__init__( *args, **kwargs )
        if pass_a_Q_object:
            self.fields['permissions'].queryset = Permission.objects.filter(pass_a_Q_object) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文