vendredi 14 novembre 2014

python - Django créer personnalisé UserCreationForm (base) - Stack Overflow


Hey guy, I'm studying Django and I love it.


I enabled the user auth module, but when I use UserCreationForm he ask me only username and the two password/password confirmation fields. I want also email and fullname fields, and set to required fields.


I've done this:


from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User

class RegisterForm(UserCreationForm):
email = forms.EmailField(label = "Email")
fullname = forms.CharField(label = "Full name")

class Meta:
model = User
fields = ("username", "fullname", "email", )

Now the form show the new fields but it doesn't save them in database.


How can I fix? Sorry for this noob question.


Thank you.




There is no such field called fullname in the User model.


If you wish to store the name using the original model then you have to store it separately as a first name and last name.


Edit: If you want just one field in the form and still use the original User model use the following:


You can do something like this:


from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User

class RegisterForm(UserCreationForm):
email = forms.EmailField(label = "Email")
fullname = forms.CharField(label = "First name")

class Meta:
model = User
fields = ("username", "fullname", "email", )

Now you have to do what manji has said and override the save method, however since the User model does not have a fullname field it should look like this:


def save(self, commit=True):
user = super(RegisterForm, self).save(commit=False)
first_name, last_name = self.cleaned_data["fullname"].split()
user.first_name = first_name
user.last_name = last_name
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user

Note: You should add a clean method for the fullname field that will ensure that the fullname entered contains only two parts, the first name and last name, and that it has otherwise valid characters.


Reference Source Code for the User Model:


http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/models.py#L201



I just signed up for an account on StackOverflow, and thus lack the points required to reply directly to chands' answer. However, be careful with the following line of code:


first_name, last_name = self.cleaned_data["fullname"].split()

This raises 'ValueError: too many values to unpack' if fullname is something like "Foo Bar Baz" (e.g. two instances of whitespace if your user has a middle name). I use a slight variation which stuffs everything after the initial whitespace into the last_name.


first_name, last_name = self.cleaned_data["fullname"].split(None, 1)



You have to override UserCreationForm.save() method:


    def save(self, commit=True):
user = super(RegisterForm, self).save(commit=False)
user.fullname = self.cleaned_data["fullname"]
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user


http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/forms.py#L10




Hey guy, I'm studying Django and I love it.


I enabled the user auth module, but when I use UserCreationForm he ask me only username and the two password/password confirmation fields. I want also email and fullname fields, and set to required fields.


I've done this:


from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User

class RegisterForm(UserCreationForm):
email = forms.EmailField(label = "Email")
fullname = forms.CharField(label = "Full name")

class Meta:
model = User
fields = ("username", "fullname", "email", )

Now the form show the new fields but it doesn't save them in database.


How can I fix? Sorry for this noob question.


Thank you.



There is no such field called fullname in the User model.


If you wish to store the name using the original model then you have to store it separately as a first name and last name.


Edit: If you want just one field in the form and still use the original User model use the following:


You can do something like this:


from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User

class RegisterForm(UserCreationForm):
email = forms.EmailField(label = "Email")
fullname = forms.CharField(label = "First name")

class Meta:
model = User
fields = ("username", "fullname", "email", )

Now you have to do what manji has said and override the save method, however since the User model does not have a fullname field it should look like this:


def save(self, commit=True):
user = super(RegisterForm, self).save(commit=False)
first_name, last_name = self.cleaned_data["fullname"].split()
user.first_name = first_name
user.last_name = last_name
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user

Note: You should add a clean method for the fullname field that will ensure that the fullname entered contains only two parts, the first name and last name, and that it has otherwise valid characters.


Reference Source Code for the User Model:


http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/models.py#L201


I just signed up for an account on StackOverflow, and thus lack the points required to reply directly to chands' answer. However, be careful with the following line of code:


first_name, last_name = self.cleaned_data["fullname"].split()

This raises 'ValueError: too many values to unpack' if fullname is something like "Foo Bar Baz" (e.g. two instances of whitespace if your user has a middle name). I use a slight variation which stuffs everything after the initial whitespace into the last_name.


first_name, last_name = self.cleaned_data["fullname"].split(None, 1)


You have to override UserCreationForm.save() method:


    def save(self, commit=True):
user = super(RegisterForm, self).save(commit=False)
user.fullname = self.cleaned_data["fullname"]
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user


http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/forms.py#L10



0 commentaires:

Enregistrer un commentaire