我使用的是用户模型
from django.contrib.auth.models import User
UserProfile模型扩展了用户模型
^{pr2}$我需要取用户的头像,我在做类似的事情
user = User.objects.get(pk=1)
user.userprofiles
但这让我错了
RelatedObjectDoesNotExist: User has no userprofile.
跟踪:
In [5]: user = User.objects.get(pk=1)
In [6]: user.userprofiles
---------------------------------------------------------------------------
RelatedObjectDoesNotExist Traceback (most recent call last)
<ipython-input-6-2253b19e792d> in <module>()
----> 1 user.userprofiles
C:\Program Files\Anaconda3\lib\site-packages\django\db\models\fields\related_des
criptors.py in __get__(self, instance, cls)
405 "%s has no %s." % (
406 instance.__class__.__name__,
--> 407 self.related.get_accessor_name()
408 )
409 )
RelatedObjectDoesNotExist: User has no userprofiles.
您在以下内容中出现了一个错误:
但是,您尝试使用
user.userprofile
而不使用s来访问它要么删除
related_name='userprofiles'
中的s
,要么继续移动,而是通过添加s
:>;用户.userprofiles只是数据库中没有该用户的UserProfile。
RelatedObjectDoesNotExist
自己说)要从
User
对象获取UserProfile
对象,需要使用userprofiles
相关问题 更多 >
编程相关推荐