2

On the Django website this sample code is given:

>>> class RestrictedArticleForm(EnhancedArticleForm):
...     class Meta(ArticleForm.Meta):
...         exclude = ('body',)

My understanding of this is that there's a modelform called EnhancedArticleForm (or ArticleForm and EnhancedArticleForm) and that this should exclude the body field from the form when it's rendered. My code looks like this:

class EditUserForm(UserForm):
    class Meta(UserForm.Meta):
        exclude = ('username',)

I don't want the user to be able to change their username obviously. But with this code in place, all it does is make the username field the last field to be displayed. It doesn't actually exclude it. Am I missing something obvious?

Edit:

Apparently this is because of a bug in django. I'm trying to overwrite init like so but the form doesn't show up. I think it's because I maybe did this wrong:

class EditUserForm(UserForm):
    def __init__(self,instance):
        UserForm.__init__(self,instance)
        del self.fields['username']

1 Answer 1

1

This is actually a bug in Django:

http://code.djangoproject.com/ticket/8620 (See the comment in the ticket further down for your situation)

Unfortunately, it looks like it hasn't seen any action in over a year.

One way around this is to override the forms __init__ method and simply remove that field from self.fields.

Sign up to request clarification or add additional context in comments.

5 Comments

Interesting stuff. JPC did you override username in your UserForm?
Yes because I needed to change some of its behavior
I tried to override the forms init method but now the form won't even show up. I edited my code to show this
What's the default value for a modelform when you don't pass request.POST
...so that it doesn't try to always validate my form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.