我希望管理员能够根据所述对象可用的权限,为一个人分配/删除django-guardian权限以及它们与Project对象的关系。
屏幕看起来像这样:
项目在URL中提供,视图可以看到。我创建了一个自定义表单,它将项目作为输入,如下所示:
from django import forms
from django.contrib.auth.models import User
from guardian.shortcuts import get_perms_for_model
class ProjectAssignmentForm(forms.Form):
def __init__(self, *args, **kwargs):
self.p = kwargs.pop('project')
self.perms = [(x, x) for x in get_perms_for_model(self.p)]
super(ProjectAssignmentForm, self).__init__(*args, **kwargs)
self.fields['permission'].widget = forms.CheckboxSelectMultiple()
user = forms.ModelChoiceField(queryset=User.objects.all())
permission = forms.ChoiceField(choices=self.perms)
这当前给了我一个NameError,我想因为我在self
方法之外使用__init__
。我做错了什么?
答案 0 :(得分:0)
from django import forms
from django.contrib.auth.models import User
from guardian.shortcuts import get_perms_for_model
class ProjectAssignmentForm(forms.Form):
def __init__(self, *args, **kwargs):
super(ProjectAssignmentForm, self).__init__(*args, **kwargs)
self.fields['permission'].widget = forms.CheckboxSelectMultiple(
choices=[(x, x) for x in get_perms_for_model(kwargs.pop('project'))])
user = forms.ModelChoiceField(queryset=User.objects.all())
permission = forms.MultipleChoiceField()