1 from django
.apps
import AppConfig
, apps
2 from django
.contrib
.auth
import get_user_model
3 from django
.db
.models
.signals
import post_save
6 def create_missing_profile(sender
, **kwargs
):
7 """Creates a UserProfile if a User doesn't have one"""
8 user
= kwargs
["instance"]
10 if not hasattr(user
, "profile"):
11 UserProfile
= apps
.get_model("users.UserProfile")
12 profile
= UserProfile
.objects
.create(user
=user
)
13 user
.profile
= profile
16 class UsersConfig(AppConfig
):
18 verbose_name
= "Users and Clients"
21 User
= get_user_model()
22 post_save
.connect(create_missing_profile
, sender
=User
)