I'm trying to figure out a way on how to exclude the username and/or password from the UserChangeForm
. I tried both exclude
and fields
but I doesn't work for these two fields.
我正在试图找出如何从UserChangeForm中排除用户名和/或密码的方法。我尝试了排除和字段,但我不适用于这两个字段。
Here's some code:
这是一些代码:
class ArtistForm(ModelForm):
class Meta:
model = Artist
exclude = ('user',)
class UserForm(UserChangeForm):
class Meta:
model = User
fields = (
'first_name',
'last_name',
'email',
)
exclude = ('username','password',)
def __init__(self, *args, **kwargs):
self.helper = FormHelper
self.helper.form_tag = False
super(UserForm, self).__init__(*args, **kwargs)
artist_kwargs = kwargs.copy()
if kwargs.has_key('instance'):
self.artist = kwargs['instance'].artist
artist_kwargs['instance'] = self.artist
self.artist_form = ArtistForm(*args, **artist_kwargs)
self.fields.update(self.artist_form.fields)
self.initial.update(self.artist_form.initial)
def clean(self):
cleaned_data = super(UserForm, self).clean()
self.errors.update(self.artist_form.errors)
return cleaned_data
def save(self, commit=True):
self.artist_form.save(commit)
return super(UserForm, self).save(commit)
2 个解决方案
#1
10
You won't be able to do this if you use UserChangeForm
.
如果使用UserChangeForm,则无法执行此操作。
See this https://github.com/django/django/blob/master/django/contrib/auth/forms.py.
请参阅https://github.com/django/django/blob/master/django/contrib/auth/forms.py。
You will notice that UserChangeForm
on this page explicitly defines username
and password
. These fields are present in variable declared_fields
on the form.
您会注意到此页面上的UserChangeForm明确定义了用户名和密码。这些字段存在于表单上的变量declared_fields中。
exclude
and fields
only work on fields which are taken from model
defined in Meta
. If you explicitly define some field i.e declared_fields
, they will be present on the form even if they have been excluded using exclude
. So, these fields show for you.
exclude和fields仅适用于从Meta中定义的模型中获取的字段。如果您明确定义了某些字段,即declared_fields,即使它们已使用exclude排除,它们也将出现在表单上。所以,这些字段为您显示。
To read more on this check __new__
of ModelFormMetaclass
at https://github.com/django/django/blob/master/django/forms/models.py
要了解更多信息,请访问https://github.com/django/django/blob/master/django/forms/models.py查看ModelFormMetaclass的__new__。
Workaround:
Do not use UserChangeForm and read its code. It doesn't provide you much. You can write your own form which only extends from ModelForm
and set Meta Model
as user. Copy the parts from UserChangeForm
in your form.
不要使用UserChangeForm并阅读其代码。它并没有为你提供太多。您可以编写自己的表单,该表单仅从ModelForm扩展并将Meta Model设置为用户。从表单中的UserChangeForm复制部件。
class UserForm(forms.ModelForm):
class Meta:
model = User
def __init__(self, *args, **kwargs):
#copy any functionality you want form UserChangeForm
def clean_password(self):
#copy functionality provided by UserChangeForm
#2
0
i think it may help you ...
我认为它可以帮助你......
while iterating over form field in template,write
在模板中迭代表单字段时,写入
if for field in UserChangeFormObject:
{% ifnotequal field.lable password %}
#process your form field here
{% endifnotequal%}
Read more here https://docs.djangoproject.com/en/1.6/releases/1.2/
在这里阅读更多内容https://docs.djangoproject.com/en/1.6/releases/1.2/
#1
10
You won't be able to do this if you use UserChangeForm
.
如果使用UserChangeForm,则无法执行此操作。
See this https://github.com/django/django/blob/master/django/contrib/auth/forms.py.
请参阅https://github.com/django/django/blob/master/django/contrib/auth/forms.py。
You will notice that UserChangeForm
on this page explicitly defines username
and password
. These fields are present in variable declared_fields
on the form.
您会注意到此页面上的UserChangeForm明确定义了用户名和密码。这些字段存在于表单上的变量declared_fields中。
exclude
and fields
only work on fields which are taken from model
defined in Meta
. If you explicitly define some field i.e declared_fields
, they will be present on the form even if they have been excluded using exclude
. So, these fields show for you.
exclude和fields仅适用于从Meta中定义的模型中获取的字段。如果您明确定义了某些字段,即declared_fields,即使它们已使用exclude排除,它们也将出现在表单上。所以,这些字段为您显示。
To read more on this check __new__
of ModelFormMetaclass
at https://github.com/django/django/blob/master/django/forms/models.py
要了解更多信息,请访问https://github.com/django/django/blob/master/django/forms/models.py查看ModelFormMetaclass的__new__。
Workaround:
Do not use UserChangeForm and read its code. It doesn't provide you much. You can write your own form which only extends from ModelForm
and set Meta Model
as user. Copy the parts from UserChangeForm
in your form.
不要使用UserChangeForm并阅读其代码。它并没有为你提供太多。您可以编写自己的表单,该表单仅从ModelForm扩展并将Meta Model设置为用户。从表单中的UserChangeForm复制部件。
class UserForm(forms.ModelForm):
class Meta:
model = User
def __init__(self, *args, **kwargs):
#copy any functionality you want form UserChangeForm
def clean_password(self):
#copy functionality provided by UserChangeForm
#2
0
i think it may help you ...
我认为它可以帮助你......
while iterating over form field in template,write
在模板中迭代表单字段时,写入
if for field in UserChangeFormObject:
{% ifnotequal field.lable password %}
#process your form field here
{% endifnotequal%}
Read more here https://docs.djangoproject.com/en/1.6/releases/1.2/
在这里阅读更多内容https://docs.djangoproject.com/en/1.6/releases/1.2/