6๐
If you have the AuthenticationMiddleware
enabled, you will have a user
object in all your views. To get the profile all you need to do is call user.get_profile
in your view. For example, to output the id of the profile, you would do {{ user.get_profile.id }}
.
If you would prefer not to call the get_profile function of the user object each time, you can add arbitrary items to your request. You would create a new middleware which would simply set
request.user_profile = request.user.get_profile()
Then just register that middleware in your settings.py and you should be good to go. I have used this method in the past for getting user geolocation data pinned to the request object.
0๐
This proposal depends on the assumption that userprofile objects only matter when users are already logged in so you can get the logged in user via request.user
.
It should be possible to get the userprofile by travelling the foreignkey key relation in reverse like this:
if request.user.is_authenticated():
request.user.userprofile_object_set.all() #gets all related userprofile objects
else:
#...
- [Django]-Two tables with foreign key references back to each other
- [Django]-Viewflow.io: How can I present a custom form for gathering input for a specific task?