2
我得到這個錯誤:Tastypie屬性及相關名稱,空屬性錯誤
The object '' has an empty attribute 'posts' and doesn't allow a default or null value.
我試圖讓一個職位的「票」的數量,並在我的models.py返回它:
class UserPost(models.Model):
user = models.OneToOneField(User, related_name='posts')
date_created = models.DateTimeField(auto_now_add=True, blank=False)
text = models.CharField(max_length=255, blank=True)
def get_votes(self):
return Vote.objects.filter(object_id = self.id)
這裏是我的資源:
class ViewPostResource(ModelResource):
user = fields.ForeignKey(UserResource,'user',full=True)
votes= fields.CharField(attribute='posts__get_votes')
class Meta:
queryset = UserPost.objects.all()
resource_name = 'posts'
authorization = Authorization()
filtering = {
'id' : ALL,
}
我在做什麼錯?
你的第二個解決方案工作,雖然我有點curious-請問我resources.py知道get_votes指的是在我的models.py的方法?這就是Tastypie的做法嗎? – arooo
如果您使用來自tastypie的ModelResources,則「屬性」會受到模型屬性的影響。準確地說,它是在現場脫水過程中完成的,在這裏: https://github.com/toastdriven/django-tastypie/blob/master/tastypie/fields.py#L102 – aniav