Skip to content

Signals

create_user_profile(sender, instance, created, **kwargs)

Creates or updates a user profile when a User object is saved.

This signal handler automatically creates a Profile object for newly created User instances, or updates existing Profile objects for existing users. The Profile model can be customized via settings.PROFILE_MODEL.

Parameters:

Name Type Description Default
sender

The model class that sent the signal (User).

required
instance

The actual instance of the User model being saved.

required
created bool

True if a new record was created, False for updates.

required
**kwargs

Additional keyword arguments passed by the signal.

{}

Returns:

Type Description

None

Note

The Profile model is determined by settings.PROFILE_MODEL, defaulting to 'django_util.Profile' if not specified.

Source code in django_util/signals.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    """Creates or updates a user profile when a User object is saved.

    This signal handler automatically creates a Profile object for newly created
    User instances, or updates existing Profile objects for existing users.
    The Profile model can be customized via settings.PROFILE_MODEL.

    Args:
        sender: The model class that sent the signal (User).
        instance: The actual instance of the User model being saved.
        created (bool): True if a new record was created, False for updates.
        **kwargs: Additional keyword arguments passed by the signal.

    Returns:
        None

    Note:
        The Profile model is determined by settings.PROFILE_MODEL, defaulting to
        'django_util.Profile' if not specified.
    """
    # Get the Profile model from settings or use default
    profile_model_path = getattr(settings, "PROFILE_MODEL", "django_util.Profile")
    Profile = apps.get_model(profile_model_path)

    if created:
        Profile.objects.create(user=instance)
    else:
        instance.profile.save()