Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to retrieve user with userprofile in Django

In Django, on a recommended setup, a UserProfile instance is linked by a OneToOneField with its User instance.

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    data = ...

What is the most efficient way inside a view for retrieving both, user and profile? Can I do a select_related() inner join query, to get both objects with one database hit? Or does it always come down to two separate calls? Possibly, Django's auth middleware retrieves the user instance even before the view is called ... does anybody know?

like image 301
Simon Steinberger Avatar asked Nov 13 '22 02:11

Simon Steinberger


1 Answers

The user profile can be retrieved using get_profile().

See documentation: https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

like image 102
Köver Avatar answered Dec 18 '22 08:12

Köver