I try to extend django auth user model with OneToOneField but couldn't solve this problem.
duplicate key value violates unique constraint "users_profile_user_id_key" DETAIL: Key (user_id)=(67) already exists.
I look through this issue , some people say the db out of sync.
views.py
def create_profile(request):
if request.method == 'POST':
user_form = UserRegistrationForm(request.POST)
profile_form = UserProfileForm(request.POST)
if user_form.is_valid() and profile_form.is_valid():
registration = user_form.save()
profile = profile_form.save(commit=False)
profile.user = registration
profile.save()
return redirect('index')
else:
user_form = UserRegistrationForm()
profile_form = UserProfileForm()
return render(request, 'registration/registration.html',
{'user_form': user_form,
'profile_form': profile_form})
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_image = models.ImageField(upload_to="blog/assets",
default="blog/assets/people-photo.jpg",
null=True)
birth_date = models.DateField(null=True, blank=True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
forms.py
class UserRegistrationForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'username', 'password')
widgets = {
'password': forms.PasswordInput(),
}
class UserProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('user_image', 'birth_date',)