Reputation: 28936
I am creating a site with two types of user profiles (DoctorProfile and PatientProfile). Each extend UserProfile, which has a OneToOneField relationship with User.
While attempting to register new users via a ModelForm, I encounter the following error:
duplicate key value violates unique constraint "accounts_userprofile_user_id_key"
DETAIL: Key (user_id)=(38) already exists.
This appears to happen when the PatientProfile is created, it attempts to create a user. However, the user was already created using User.objects.create_user()
How can I configure this so that I can create the user only once?
Here is my (stripped down) forms.py:
class PatientProfileForm(ModelForm):
supplied_email = forms.CharField(max_length=256, label="Email Address", required=True)
supplied_password = forms.CharField(max_length=256, label="Password", required=True, widget=forms.PasswordInput())
supplied_password_confirm = forms.CharField(max_length=256, label="Password (again)", required=True, widget=forms.PasswordInput())
def save(self, profile_callback=None):
master_patient_profile = MasterPatientProfile(user=User.objects.create_user(username=self.cleaned_data['supplied_email'], email=self.cleaned_data['supplied_email'], password=self.cleaned_data['supplied_password']))
master_patient_profile.save()
A shortened version of models.py:
class UserProfile(models.Model):
user = models.OneToOneField(User, null=True)
address_line_1 = models.CharField(_('Address 1'), max_length=100)
address_line_2 = models.CharField(_('Address 2'), max_length=100, blank=True)
city = models.CharField(_('City'), max_length=50)
state = models.CharField(_('State'), max_length=20)
zipcode = models.CharField(_('ZIP Code'), max_length=5)
class PatientProfile(UserProfile):
gender = models.ForeignKey(UserGender)
phone_number = models.CharField(max_length=12)
date_of_birth = models.DateField(null=True)
class DoctorProfile(UserProfile):
specialty = models.ManyToManyField(DoctorSpecialty)
professional_statement = models.TextField()
EDIT: (based on Chris Pratt's suggestion)
The form's save()
method now looks like this:
new_user, created = User.objects.get_or_create(username=self.cleaned_data['supplied_email'], email=self.cleaned_data['supplied_email'])
if created:
new_user.set_password(self.cleaned_data['supplied_password'])
master_patient_profile = MasterPatientProfile(user=new_user)
Upvotes: 0
Views: 3494
Reputation: 239440
Don't use create_user
. That's purely a convenience method and is only really useful for the actual User
creation form.
Everywhere else, you should be using get_or_create
. The only drawback is that you can't set the password with that, so you need to do it in two steps.
user, created = User.objects.get_or_create(username=self.cleaned_data['supplied_email'], email=self.cleaned_data['supplied_email'])
if created:
user.set_password(self.cleaned_data['supplied_password'])
Upvotes: 2