开发者

Django extending user model and displaying form

I am writing website and i`d like to implement profile managment. Basic thing would be to e开发者_开发知识库dit 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.


You should create another form, that excludes the fields you don't want (or simply don't specify them in the fields list). Then pass the 2 different forms to the registration and edit-profile views.


Try removing 'username' and 'email' from fields in Meta:

class Meta: 
    model = User 
    fields = ('first_name', 'last_name')


What i did is i created new form and used it and it worked. It allows to edit fields from User model not only UserExtended. Thanks for help.

class UserProfileForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
    super(UserProfileForm, self).__init__(*args, **kwargs)
    try:            
        self.fields['first_name'].initial = self.instance.user.first_name
        self.fields['last_name'].initial = self.instance.user.last_name
        self.fields['email'].initial = self.instance.user.email
    except models.User.DoesNotExist:
        pass

email = forms.EmailField(label = "Główny adres email",
                         help_text="",
                         required=True)
first_name = forms.CharField(label = "Imię",
                             required=False)
last_name = forms.CharField(label = "Nazwisko",
                            required=False)
kod_pocztowy = forms.RegexField('\d{2}-\d{3}',
                                required = False,
                                label="Kod pocztowy",
                                error_messages={"invalid":'Poprawna wartość to np: 41-200'})

class Meta:
    model = UserExtended
    exclude  = ('user')

def save(self, *args, **kwargs):
    u = self.instance.user
    u.email = self.cleaned_data['email']
    u.first_name = self.cleaned_data['first_name']
    u.last_name = self.cleaned_data['last_name']
    u.kod_pocztowy = self.cleaned_data['kod_pocztowy']
    u.save()
    profile = super(UserProfileForm, self).save(*args, **kwargs)
    return profile
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜