Home > Software engineering >  Extending django user model with one to one relation
Extending django user model with one to one relation

Time:08-07

Im very new to dajngo , im trying to get some more data from user and insert them in profile table my model

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=500, blank=True,null=True)
    location = models.CharField(max_length=30, blank=True,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()

my form

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


class UserRegistrationForm(UserCreationForm):
    first_name = forms.CharField(max_length=101)
    last_name = forms.CharField(max_length=101)
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2']

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields=['bio','location','birth_date']

and my view

def register(request):
    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)
        profile_form = ProfileForm(request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(request, 'Your profile was successfully updated!')
            return redirect('home')
        else:

            messages.error(request,'Please correct the error below.')
    else:
        user_form = UserRegistrationForm()
        profile_form = ProfileForm()
    return render(request, 'users/register.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })

When i try to save the form i get the error

django.db.utils.IntegrityError: NOT NULL constraint failed: users_profile.user_id

I know this is because im calling save on profile_form and it has no idea of user id instance how can i fix it though ?

CodePudding user response:

You can use commit=False to edit the object before it's saved to the database. You can then attach the profile to the user and save it, like so:

if user_form.is_valid() and profile_form.is_valid():
    user = user_form.save()
    profile = profile_form.save(commit=False)
    profile.user = user
    profile.save()

Once this is fixed you might get an error from your post_save signal as a profile is already created here and there's a one-to-one relationship.

CodePudding user response:

This is how i ended up solving the problem, most example over the internet was about the user account already created and using existing user instance, anyway i made following changes :

views.py

def register(request):
    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)
        profile_form = ProfileForm(request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            #user.refresh_from_db()
            user.profile.bio=profile_form.cleaned_data.get('bio')
            user.profile.location=profile_form.cleaned_data.get('location')
            user.save()
            messages.success(request, 'Your account successfully created!')
            return redirect('home')
        else:

            messages.error(request,'Please correct the error below.')
    else:
        user_form = UserRegistrationForm()
        profile_form = ProfileForm()
    return render(request, 'users/register.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })

models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=500, blank=True,null=True)
    location = models.CharField(max_length=30, blank=True,null=True)
    birth_date = models.DateField(null=True, blank=True)


@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
    else:
        instance.profile.save()

and no changes over form.py .

  • Related