Ich versuche save
Methode für benutzerdefinierte Erstellung Formular in Django zu überschreiben. Ich habe Modell UserProfile
, das viele Attribute hat. Ich habe eine UserProfileCreationForm
erstellt, die beide User
und UserProfile
erstellen und speichern sollte. Jetzt speichert es User
aber es kann UserProfile
nicht speichern (Ich habe Attribut date_of_birth
überschrieben, um wählen zu können, anstatt Datum in charfield zu schreiben).Wie überschreiben Sie die Speichermethode im benutzerdefinierten Benutzerprofilerstellungsformular ordnungsgemäß?
Also, was ich will, ist diese Form sowohl machen Speichern User
und UserProfile
oder Fehler auslösen, wenn ich UserProfileCreationForm_object.save()
Hier ist meine Form:
class UserProfileCreationForm(UserCreationForm):
username = forms.CharField(label="Username",max_length=40)
email = forms.EmailField(label="Email address", max_length=40)
first_name = forms.CharField(label='First name', max_length=40)
last_name = forms.CharField(label='Last name', max_length=40)
date_of_birth = forms.DateField(label='Date of birth',
widget=SelectDateWidget(years=[y for y in range(1930,2050)]),
required=False)
password1 = forms.CharField(label="Password", widget=forms.PasswordInput)
password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput)
class Meta():
model = UserProfile
fields = ('username','email','first_name','last_name','password1','password2','date_of_birth','telephone','IBAN',)
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
msg = "Passwords don't match"
raise forms.ValidationError("Password mismatch")
return password2
def save(self, commit=True):
user = User(username=self.cleaned_data['username'],
first_name=self.cleaned_data['first_name'],
last_name=self.cleaned_data['last_name'],
email=self.cleaned_data['email'])
user.save()
# user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
user_profile = super(self).save()
if commit:
user.save()
user_profile.save()
return user
Falls es notwendig ist, hier ist die Modell:
class UserProfile(models.Model):
# ATRIBUTY KTORE BUDE MAT KAZDY
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_profile')
iban = models.CharField(max_length=40, blank=True,verbose_name='IBAN')
telephone = models.CharField(max_length=40, null=True, blank=True)
HOW_DO_YOU_KNOW_ABOUT_US_CHOICES = (
('coincidence', u'It was coincidence'),
('relative_or_friends', 'From my relatives or friends'),
)
how_do_you_know_about_us = models.CharField(max_length=40, choices=HOW_DO_YOU_KNOW_ABOUT_US_CHOICES, null=True,
blank=True)
MARITAL_STATUS_CHOICES = (
('single', 'Single'),
('married', 'Married'),
('separated', 'Separated'),
('divorced', 'Divorced'),
('widowed', 'Widowed'),
)
marital_status = models.CharField(max_length=40, choices=MARITAL_STATUS_CHOICES, null=True, blank=True)
# TRANSLATORs ATTRIBUTES
# jobs = models.Model(Job)
language_tuples = models.ManyToManyField(LanguageTuple)
rating = models.IntegerField(default=0)
number_of_ratings = models.BigIntegerField(default=0)
def __unicode__(self):
return '{} {}'.format(self.user.first_name, self.user.last_name)
def __str__(self):
return '{} {}'.format(self.user.first_name, self.user.last_name)