請幫幫我。我被困在我的django項目的一個地方,以便由老師生成問卷。我已經實現了我自己的UserAdmin模型,儘管is_staff,is_superuser字段,我有自己的布爾字段「is_teacher」。創建新用戶時,如果勾選「is_teacher」框,我想給該用戶權限來管理整個問卷模型,但刪除管理MyUser
模型(創建,更改和刪除用戶)的權限Django Admin,自定義權限複選框
這就是我所有的' VE實現:
在models.py
class MyUserManager(BaseUserManager):
def create_user(self, username, email=None, password=None):
if not username:
raise ValueError('The given username must be set')
email = MyUserManager.normalize_email(email)
user = self.model(username=username, email=email, is_staff=True, is_active=True, is_superuser=False, is_teacher=True)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, password):
u = self.create_user(username, email, password)
u.is_staff = True
u.is_active = True
u.is_superuser = True
u.is_teacher = True
u.save(using=self._db)
return u
class MyUser(AbstractBaseUser):
username = models.CharField(max_length=30, unique=True)
email = models.EmailField(blank=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
is_teacher = models.BooleanField(default=True)
objects = MyUserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def get_full_name(self):
return self.username
def get_short_name(self):
return self.username
def __unicode__(self):
return self.username
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, module):
return True
在admin.py
class UserCreationForm(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given username and
password.
"""
error_messages = {
'password_mismatch': "The two password fields didn't match.",
}
password1 = forms.CharField(label="Password",
widget=forms.PasswordInput)
password2 = forms.CharField(label="Password confirmation",
widget=forms.PasswordInput)
class Meta:
model = MyUser
fields = ("username",)
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
code='password_mismatch',
)
return password2
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
class UserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField(label="Password")
class Meta:
model = MyUser
fields = ('username', 'password', 'email', 'is_active', 'is_staff', 'is_superuser', 'is_teacher')
def clean_password(self):
# Regardless of what the user provides, return the initial value.
# This is done here, rather than on the field, because the
# field does not have access to the initial value
return self.initial["password"]
class MyUserAdmin(UserAdmin):
form = UserChangeForm
add_form = UserCreationForm
list_display = ('username', 'email', 'is_teacher')
list_filter = ('is_staff', 'is_superuser', 'is_active')
fieldsets = (
(None, {'fields': ('username', 'password')}),
('Personal info', {'fields': ('email',)}),
('Permissions', {'fields': ('is_superuser', 'is_teacher',)}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'email', 'password1', 'password2')}
),
)
search_fields = ('username', 'email')
ordering = ('username',)
filter_horizontal =()
建議你創建你所需要的權限的「教師組,然後該組中添加任何用戶,只要其'is_teacher'字段被設置爲'True'https://docs.djangoproject.com/zh/dev/topics/auth/default/#groups – Anentropic 2014-10-30 12:26:41
感謝您的建議,但我不能這樣做。它必須這樣做:超級用戶正在創建新帳戶,勾選「is_teacher」字段並且必須添加所有權限。我只是不知道該放置哪些權限以及如何對其進行編碼。 – Skiben 2014-10-30 13:33:42
https://docs.djangoproject.com/en/dev/topics/auth/default/#topic-authorization – madzohan 2014-10-30 13:48:35