在Django中使用userprofile检索用户的最有效方法

时间:2022-09-23 17:09:27

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

在Django中,在推荐的设置中,UserProfile实例由OneToOneField与其User实例链接。

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?

检索用户和配置文件的视图中最有效的方法是什么?我可以执行select_related()内连接查询,以获取一个数据库命中的两个对象吗?或者它总是归结为两个单独的呼叫?可能,Django的auth中间件甚至在调用视图之前检索用户实例...有人知道吗?

1 个解决方案

#1


1  

The user profile can be retrieved using get_profile().

可以使用get_profile()检索用户配置文件。

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

参见文档:https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

#1


1  

The user profile can be retrieved using get_profile().

可以使用get_profile()检索用户配置文件。

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

参见文档:https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users