How do I create a custom UserChangeForm that does not allow certain fields to be edited?

First: to limit which fields are in the form, follow the documentation. There are basically two options to filter fields: fields and exclude.

Second: you are always creating a new user. Initialize form with the User instance, not only you will be able to save a form, but you will have an initial data set.

Your view code should read:

# in the POST:
form = UserChangeForm(request.POST, instance=request.user)

# otherwise
form = UserChangeForm(instance=request.user)

and remove value assignment from the form.py.


This needs to be updated: You cannot exclude fields in UserChangeForm()

Too bad.


Three things. First of all, you should set self.fields['username'] to a form field, like forms.CharField(initial=self.instance.username). Second of all, you're using UserChangeForm instead of CustomUserChangeForm, in your view. And thirdly, http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form (a proper link now, not one to someone's hard drive...