类似于这个问题:Is it possible to have 2 profile models using django's AUTH_PROFILE_MODULE
但我正在尝试使用 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() 获取用户配置文件?即:如果我试图检索用户的头像以获取个人资料......
{{ comment.user.get_profile.avatar }}
这行不通。