1👍
You can access the related Profile
of a User
with:
myuser.profile
indeed, a OneToOneField
is in essence a ForeignKey
with a unique=True
constraint. This means that for a User
there is at most one related Profile
. The related_name=…
parameter [Django-doc] by default is the name of the class (here Profile
) in lowercase (so profile
). You can thus access the related Profile
with .profile
. It will raise an AttributeError
in case no such Profile
exists.
The modeling of manager however has redundant data: you do not need to specify both the User
and the Profile
, you can determine the other by the first. By introducing duplicate data, you make it possible that in the future Manager
s exist where the user
is updated, but the user_profile
is not and vice versa. If you need the profile
and you store the user
, you can access it with mymanager.user.profile
, and if you store the user_profile
, you can access it with mymanager.user_profile.user
. Storing both thus makes not much sense.
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.