I am building a DRF project and have enabled user authentication and registration using django-allauth and django-rest-auth. A new user is registered by providing username, email, password1 and password 2.
I want to create a user profile when a user is created, however he would have to enter anything related to profile while registration. User can edit this profile afterwards, whenever he wants. My user profile model:
class Profile(models.Model):
user = models.OneToOneField(User, blank=True, on_delete=models.CASCADE, )
name = models.CharField(max_length=50, blank=True, )
age = models.IntegerField(blank=True, )
...
...
and in serializers.py I have:
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = '__all__'
I saw some posts which added following methods to Profile model:
@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()
with them, user was created but not the profile, and I got error IntegrityError: (1048, "Column 'age' cannot be null")
What is the most elegant way of creating, and updating such a profile?