Search code examples
pythondjangoauthenticationdjango-modelsdjango-users

Django: When a user profile is created automatically, what is it set to?


I have a user profile which is created automatically after the User is saved:

def create_user_info(sender, instance, created, **kwargs):
    if created:
        UserInfo.objects.create(user=instance)

post_save.connect(create_user_info, sender=User)


class UserInfo(models.Model):
    user = models.OneToOneField(User)
    pen_name = models.CharField(max_length=30)
    activated = models.BooleanField()
    def __unicode__(self):
        return self.email + '-' + self.pen_name

When I create a user object with the User.objects.create_user function, what happens to the variables/fields in the user profile. Unless I'm mistaken, they can't be empty.


Solution

  • User Profile is no different from any other model. So it behaves exactly like how any other model would behave if you save it to database without specifying all fields. It will have empty fields.
    Ofcourse in your case, since you do not allow blank=True, null=True, this will raise an error and require you to specify the fields.