我尝试使用ContentTypes Framework和DRF。我已关注文档,generic relationships
我已经建立了BlogPost
模型:
class BlogPost(models.Model):
user = models.ForeignKey(User, default=1, null=True, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
comments = GenericRelation(Comment)
和Comment
型号:
class Comment(models.Model):
comment_author = models.ForeignKey(User, default=1, null=True, on_delete=models.CASCADE)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
我成立了serializers
:
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = "__all__"
class CommentedObjectRelatedField(serializers.RelatedField):
def to_representation(self, value):
"""
Serialize tagged objects to a simple textual representation.
"""
if isinstance(value, Comment):
serializer = CommentSerializer(value)
else:
raise Exception('Unexpected type of commented object')
return serializer.data
class BlogPostSerializer(serializers.HyperlinkedModelSerializer):
comments = CommentedObjectRelatedField(many=True, read_only=True)
time_since_publication = serializers.SerializerMethodField()
def get_time_since_publication(self, object):
publication_date = object.published_at
now = datetime.now()
time_delta = timesince(publication_date, now)
return time_delta
class Meta:
model = BlogPost
fields = ['id', 'pk', 'title', 'slug', 'content','comments', 'published_at', 'timestamp', 'updated', 'time_since_publication']
read_only_fields = ('pk',)
lookup_field = ('pk',)
[当我在浏览器中检查api
时,我会得到json
答案
[
{
"id": 1,
"pk": 1,
"title": "this is title",
"slug": "this-is-title",
"content": "This is content",
"comments": null,
"published_at": "2019-11-15",
"timestamp": "2019-11-15T12:14:37.336170Z",
"updated": "2019-11-15T12:14:37.336208Z",
"time_since_publication": "16 hours, 5 minutes"
}
]
我如何获得相关评论。谢谢
好像您已切换通用关系和反向通用关系。在您引用的示例中,要序列化的对象是TaggedItem
,这是具有通用外键的模型。但是在您的代码中,您尝试序列化具有BlogPost
的GenericRelation
对象(即关系的另一端)。从文档中:
注意,反向通用密钥,使用GenericRelation表示字段,可以使用常规关系字段类型进行序列化,因为关系中目标的类型总是已知的。
[尝试简单地从串行器中删除CommentedObjectRelatedField
行。让HyperlinkedModelSerializer
处理关系。
在粗略的本地尝试中,将字段留在外面是行不通的(在这种情况下,我得到了一个异常,指出“ GenericRelatedObjectManager类型的对象不可JSON序列化”)。我必须在序列化器中明确添加HyperlinkedRelatedField
。