1
我:Django with Tastypie:如何在Profile中存儲關注者,followers_count?
class Profile(models.Model):
user = models.OneToOneField(User)
birthday = models.DateField(null=True)
gender = models.CharField(max_length=140)
profile_picture = models.URLField()
class Follow(models.Model):
follower = models.ForeignKey(User, related_name="who_follows")
followed = models.ForeignKey(User, related_name="who_is_followed")
follow_time = DateTimeField(auto_now=True)
那麼應該怎麼ProfileResource,以便我能得到FOLLOWERS_COUNT?或者我可以POST那個用戶有一個新的追隨者?
我ProfileResource是:
class ProfileResource(ModelResource):
followers_count = Follow.objects.filter(followed=)
class Meta:
queryset = User.objects.all()
resource_name = 'profile'
filtering = {
'username': ALL,
}
是它的最好方法是什麼?或者我應該使用它:
class Profile(models.Model):
user = models.OneToOneField(User)
birthday = models.DateField(null=True)
gender = models.CharField(max_length=140)
profile_picture = models.URLField()
followers = ToManyField('self')
following = ToManyField('self')
謝謝。如果我需要獲取用戶的關注者列表,該怎麼辦? – Burak 2012-08-01 07:52:35
如果追隨者的列表是單獨的資源,它不會更清潔嗎? – 2012-08-01 08:06:18
如果你願意,你就需要相關的資源對象的實際列表 - 見http://django-tastypie.readthedocs.org/en/latest/resources.html#reverse-relationships – JamesO 2012-08-01 08:07:17