Exclude username or password from UserChangeForm in Django Auth -
i'm trying figure out way on how exclude username and/or password userchangeform
. tried both exclude
, fields
doesn't work these 2 fields.
here's 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)
you won't able if use userchangeform
.
see https://github.com/django/django/blob/master/django/contrib/auth/forms.py.
you notice userchangeform
on page explicitly defines username
, password
. these fields present in variable declared_fields
on form.
exclude
, fields
work on fields taken model
defined in meta
. if explicitly define field i.e declared_fields
, present on form if have been excluded using exclude
. so, these fields show you.
to read more on check __new__
of modelformmetaclass
@ https://github.com/django/django/blob/master/django/forms/models.py
workaround:
do not use userchangeform , read code. doesn't provide much. can write own form extends modelform
, set meta model
user. copy parts userchangeform
in form.
class userform(forms.modelform): class meta: model = user def __init__(self, *args, **kwargs): #copy functionality want form userchangeform def clean_password(self): #copy functionality provided userchangeform
Comments
Post a Comment