Django extending user model and displaying form

Posted by MichalKlich on Stack Overflow See other posts from Stack Overflow or by MichalKlich
Published on 2010-04-07T19:39:45Z Indexed on 2010/04/07 19:43 UTC
Read the original article Hit count: 237

Hello,

I am writing website and i`d like to implement profile managment. Basic thing would be to edit some of user details by themself, like first and last name etc. Now, i had to extend User model to add my own stuff, and email address.

I am having troubles with displaying form. Example will describe better what i would like achieve.

This is mine extended user model.

class UserExtended(models.Model): 
    user = models.ForeignKey(User, unique=True) 
    kod_pocztowy = models.CharField(max_length=6,blank=True) 
    email  = models.EmailField()

This is how my form looks like.

class UserCreationFormExtended(UserCreationForm): 
    def __init__(self, *args, **kwargs): 
        super(UserCreationFormExtended, self).__init__(*args, **kwargs)       
        self.fields['email'].required = True
        self.fields['first_name'].required = False
        self.fields['last_name'].required = False
    class Meta: 
        model = User 
        fields = ('username', 'first_name', 'last_name', 'email')

It works fine when registering, as i need allow users to put username and email but when it goes to editing profile it displays too many fields. I would not like them to be able to edit username and email. How could i disable fields in form?

Thanks for help.

© Stack Overflow or respective owner

Related posts about django-forms

Related posts about django-models