我有以下模型設置。嘗試並展開contrib.auth.user模型並添加「關係」管理
from django.db import models
from django.contrib.auth.models import User
class SomeManager(models.Manager):
def friends(self):
# return friends bla bla bla
class Relationship(models.Model):
"""(Relationship description)"""
from_user = models.ForeignKey(User, related_name='from_user')
to_user = models.ForeignKey(User, related_name='to_user')
has_requested_friendship = models.BooleanField(default=True)
is_friend = models.BooleanField(default=False)
objects = SomeManager()
relationships = models.ManyToManyField(User, through=Relationship, symmetrical=False)
relationships.contribute_to_class(User, 'relationships')
在這裏,我採取用戶對象,並使用contribute_to_class添加'關係'到用戶對象。關係顯示,但如果調用User.relationships.friends它應該運行friends()方法,但它的失敗。任何想法我會如何做到這一點?
感謝
爲什麼不使用用戶配置文件功能? http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users這應該是向`User`類添加特徵的正確方法。 – 2011-01-06 22:57:21
我希望將其作爲「應用程序」發佈,而不是每個用戶都有配置文件模塊設置。 – dotty 2011-01-06 23:05:52