[Answered ]-Creating & Updating UserProfiles in Django

2πŸ‘

βœ…

how would I first create, then update all of this, before finally saving

These aren’t separate steps. When you create or update a record in Django, you are saving it to the database.

For the registration form, I’d recommend you set it up as a ModelForm on User records, then specify the additional fields you want to save to the profile and save them separately in the save function, like so…

class RegistrationForm(forms.ModelForm):
    location = forms.CharField(max_length=100)
    # etc -- enter all the forms from UserProfile here

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email', and other fields in User ]

    def save(self, *args, **kwargs):
        user = super(RegistrationForm, self).save(*args, **kwargs)
        profile = UserProfile()
        profile.user = user
        profile.location = self.cleaned_data['location']
        # and so on with the remaining fields
        profile.save()
        return profile
πŸ‘€Jordan Reiter

0πŸ‘

You could call profile.user.save() and after it profile.save() when you need to save data from registration form.

πŸ‘€dbf

Leave a comment