[Answered ]-Writing user.get_profile() custom function which should create auth profile if there is not any

1๐Ÿ‘

โœ…

I guess that you have a model to handle user profile like this:

class UserProfile(models.Model):
    """Contains user profile fields not provided by User model"""
    user = models.OneToOneField(User)
    # Defined User profile fields like picture, phone, etc

So adding following line (maybe in your models.py after UserProfile model):

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

Allows access to the profile of a user (i.e. in templates: {% user.profile.phone %}) by creating it if not exists.

This is the way I solved in my site a problem like you describe.

Hope this helps

๐Ÿ‘คjuliocesar

1๐Ÿ‘

I am a bit confused. Are you trying to let users create account and sign in? Then use django-registration which is easy and works out of the box.

๐Ÿ‘คuser2707389

Leave a comment