Skip to content

Commit

Permalink
create profile if none exists when trying to save User
Browse files Browse the repository at this point in the history
  • Loading branch information
jchate6 committed Dec 3, 2024
1 parent 5f7cf90 commit f046f4e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tom_common/signals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.db.models.signals import post_save, pre_delete
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from tom_common.models import Profile
Expand All @@ -14,4 +14,9 @@ def create_profile(sender, instance, created, **kwargs):
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
"""When a user is saved, save their profile."""
instance.profile.save()
# Take advantage of the fact that logging in updates a user's last_login field
# to create a profile for users that don't have one.
try:
instance.profile.save()
except User.profile.RelatedObjectDoesNotExist:
Profile.objects.create(user=instance)

0 comments on commit f046f4e

Please sign in to comment.