[英]Overriding UserChangeForm in django
在這種情況下,您甚至不需要使用UserChangeForm
。 請參閱該類的源代碼:
class UserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField(
label=_("Password"),
help_text=_(
"Raw passwords are not stored, so there is no way to see this "
"user's password, but you can change the password using "
"<a href=\"{}\">this form</a>."
),
)
class Meta:
model = User
fields = '__all__'
field_classes = {'username': UsernameField}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
password = self.fields.get('password')
if password:
password.help_text = password.help_text.format('../password/')
user_permissions = self.fields.get('user_permissions')
if user_permissions:
user_permissions.queryset = user_permissions.queryset.select_related('content_type')
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"]
90%的額外代碼與您不需要的密碼有關,其中一些與權限和用戶名有關。 因此,對於您的需求,僅擴展ModelForm
就足夠了。
from django.contrib.auth.models import User
from django.forms import ModelForm
class UserChangeForm(ModelForm):
class Meta:
model = User
fields = ('email',)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.