類似這樣的問題:Is it possible to have 2 profile models using django's AUTH_PROFILE_MODULEAUTH形象模塊問題,多個配置文件
但我試圖使用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 }}
這是行不通的。
你是說用戶可以有多個配置文件實例嗎?或者多個類型的配置文件,每個配置文件具有不同的屬性? – Meitham 2012-07-25 21:41:53
你能解釋一下「不起作用」嗎?它會拋出一個錯誤嗎?你沒有看到你期望的輸出嗎? UserProfile似乎沒有頭像字段,這似乎可能會導致問題在這裏。 – girasquid 2012-07-25 21:42:24
@Meitham每個用戶可以擁有多個具有不同屬性的配置文件類型。 (我對此感到抱歉) – Modelesq 2012-07-25 21:44:08