Python 具有多个配置文件的身份验证配置文件模块问题

Python 具有多个配置文件的身份验证配置文件模块问题,python,django,django-models,django-templates,Python,Django,Django Models,Django Templates,与这个问题类似: 但是我正在尝试使用django模板标记来获得我需要的东西 共有2个配置文件: class Profile1(models.Model): user = models.ForeignKey(User, null=True, unique=True) avatar = models.ImageField(upload_to='avatars/users/', null=True, blank=True) ... More fields ... class P

与这个问题类似:

但是我正在尝试使用django模板标记来获得我需要的东西

共有2个配置文件:

class Profile1(models.Model):
    user = models.ForeignKey(User, null=True, unique=True)
    avatar = models.ImageField(upload_to='avatars/users/', null=True, blank=True)
    ... More fields ...
class Profile2(models.Model):
    user = models.ForeignKey(User, null=True, unique=True)
    avatar = models.ImageField(upload_to='avatars/users/', null=True, blank=True)
    ... More fields ...

AUTH_PROFILE_MODULE = 'profiles.UserProfile'

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    profile1 = models.ForeignKey(Profile1, related_name="profile1")
    profile2 = models.ForeignKey(Profile2, related_name="profile2")
    # Each user can essentially have more than one profile.
那么,如何在模板标记中使用get_profile()获取用户配置文件?Ie:如果我试图检索用户的个人资料头像

{{ comment.user.get_profile.avatar }}

这不起作用。

如果在模型
Profile1
中定义了avator,那么您可以执行
{{comment.user.get_profile.Profile1.avatar}

get\u profile
将返回
UserProfile
实例

如果我是你,我会在个人档案中添加相关的_名称,如下所示:

class Profile1(models.Model):
    user = models.ForeignKey(User, null=True, unique=True, related_name='profile_1')
    ... More fields ...
class Profile2(models.Model):
    user = models.ForeignKey(User, null=True, unique=True, related_name='profile_2')
    ... More fields ...

然后做
user.profile\u 1.avatar
而不是通过
get\u profile()

你是说一个用户可以有多个配置文件实例吗?或者一个以上的配置文件类型,其中每个配置文件都有不同的属性?您能再解释一下“不起作用”吗?它会抛出错误吗?您没有看到您期望的输出吗?UserProfile似乎没有avatar字段,这可能会在这里造成问题。@Meitham每个用户可以有多个具有不同属性的配置文件类型。(我为混淆道歉)@girasquid没有错误。它不显示化身。并链接它:
。。。它返回相同的页面。它没有显示头像,因为这些模型都没有头像字段-例如,如果你的头像在profile1上,你需要使用
{{comment.user.get_profile.profile1.avatar}
。我接受了你的建议。尝试了两种建议。。。不幸的是,它仍然返回一个空白的img
也许我在某个地方犯了一个我无法发现的错误?请通过
manage.py shell尝试它
可能没有图像或get\u absolute\u url没有实现。在shell中尝试。返回带有化身的所有配置文件。它们是存在的。至于get_absolute_url:在这一点上我并不太担心\我不知道这是否重要。但是当
AUTH\u PROFILE\u模块='profiles.Profile1'
并使用
get\u PROFILE()
时,我可以成功地抓取化身。只是
UserProfile
把一切都搞砸了。整个事情是,有多个配置文件。所以我需要
UserProfile
引用所有可能连接到
user