Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude username or password from UserChangeForm in Django Auth

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.

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)
like image 919
codingjoe Avatar asked May 02 '13 11:05

codingjoe


2 Answers

For Django v2 just set password to None.

class UserForm(UserChangeForm):
    password = None

    class Meta:
        model = User
        fields = (
            'first_name',
            'last_name',
            'email',
            )
like image 180
Igor Jerosimić Avatar answered Nov 08 '22 06:11

Igor Jerosimić


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/

like image 44
Ravi Avatar answered Nov 08 '22 07:11

Ravi