8👍
You defined a ForeignKey
from a Profile
to a User
, but that means every Profile
is attached to a User
, it is however perfectly possible that multiple Profile
s are attached to the same User
.
Hence if you use a_user.profile
, it will not return a single Profile
, but a RelatedManager
: a manager that manages the collection (that can contain zero, one or multiple elements) of Profile
.
I think however that you actually want to associated Profile
s with distinct User
s, in which case you better use a OneToOneField
:
class Profile(models.Model):
user = models.OneToOneField(User, unique=True, on_delete=models.CASCADE)
If you then query someuser.profile
you will either get a Profile
instance, or it will raise a Profile.DoesNotExists
error in case no profile is associated with that user.
By default the related_name
of a OneToOneField
is the name of the class in lowercase, so you no longer need to specify this.
Note however that in your code instance.profile.save()
is not necessary, since the Profile
, is basically already saved by creating it:
@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
# unnecessary
# instance.profile.save()