Sto cercando di capire come escludere il nome utente e/o la password da UserChangeForm
. Ho provato sia exclude
e fields
ma non lavoro per questi due campi.Escludere nome utente o password da UserChangeForm in Django Auth
Ecco po 'di codice:
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)
Ok che ha fatto il lavoro, ma ora username e password diventano NULL ogni volta che chiamo su 'save()'. Inoltre, l'argomento della mia estesa classe utente non viene salvato. – codingjoe