Search code examples
pythondjangosqlitedjango-database

django - linking the register form info to user profile model


I am trying to make a webapp that includes register/login operations. The login is working fine but the registration form is frustrating.

I have a class registration form that inherits from UserCreationForm. The users are created and seen in the admin page when created but the problem I am having is not being able to see them in my UserProfile model in the admin page. It does not link the information and I could not find a way the link them.

Here is the registration form:

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

    class RegistrationForm(UserCreationForm):
        email = forms.EmailField(required=True)
        city = forms.CharField(required=False)
        country = forms.CharField(required=True)
        first_name = forms.CharField(required=True)
        last_name = forms.CharField(required=True)
        username = forms.CharField(required=True)

        class Meta:
            model = User
            fields = (
                'username',
                'first_name',
                'last_name',
                'country',
                'city',
                'email'
            )

            def save(self, commit=True):
                user = super(RegistrationForm, self).save(commit=False)
                #user.first_name = self.cleaned_data['first_name']

                if commit:
                    user.save()

                return user

Here are my models:

class UserProfile(models.Model):
    user = models.OneToOneField( User,
                                on_delete=models.CASCADE
                                )
    username = models.TextField(max_length=30, default="")
    first_name = models.TextField(max_length=30, default="")
    last_name = models.TextField(max_length=30, default="")
    country = models.TextField(max_length=30, default="Which country are you from?")
    city = models.TextField(max_length=30, default="Which city are you from?")


class ColorChoice(models.Model):
    user = models.ForeignKey(
                            'UserProfile',
                            on_delete=models.CASCADE
                            )
    color1 = models.IntegerField()
    color2 = models.IntegerField()
    color3 = models.IntegerField()
    color4 = models.IntegerField()
    color5 = models.IntegerField()

and my view.py that does the registering:

def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('color')
    else:
        form = RegistrationForm()

        args = {'form': form}
        return render(request, 'account/create_new.html', args)

Solution

  • Your form is only creating a new objects for User. You would need to add a signal for example to create a new object for UserProfile.

    Something like this:

    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            Profile.objects.create(user=instance)
    signals.post_save.connect(create_user_profile, sender=User)