【发布时间】:2015-03-27 07:03:38
【问题描述】:
假设我有以下序列化程序。
class ArticleSerializer(serializers.ModelSerializer):
comment_count = serializers.SerializerMethodField()
commented = serializers.SerializerMethodField()
def get_comment_count(self, obj):
# Assume the method can retrieve the comment count correctly
return x
def get_commented(self, obj):
# Return True if comment count > 0, else False
class Meta:
model = Article
fields = ['title', 'content', 'comment_count', 'commented']
对get_commented 方法中的编码有什么建议吗?我编写了类似return comment_count > 0 的代码但失败了。
【问题讨论】:
-
怎么样:
return self.get_comment_count(obj) > 0?
标签: python django serialization django-rest-framework