我正在尝试使用 django 活动流创建新闻源。我正在制作一个目标流,但如果它是不保存的评论,则不会保存操作对象
"id": 14,
"actor": {
"id": 1,
"email": "zacmwangi94@gmail.com",
"first_name": "",
"last_name": ""
},
"verb": "commented on",
"action_object": null,
"target": {
"id": 6,
"venue": {
"id": 1,
"name": "IMAX",
"address": "NAIROBI",
"city": "NAIROBI",
"rating": "1.1"
},
"thumbnail": null,
"user": 1,
"event_pic_url": null,
"name": "A movie",
"time": "2017-11-11T16:02:00Z",
"description": "A description",
"event_type": "Movie",
"invite_only": true,
"free": false,
"age_restriction": true,
"ticket_price": 1000.0,
"created_at": "2017-03-19T10:35:41.723753Z"
}
}
当动作对象是照片时,它会被正确保存。这是我的评论模型:
class Comment(models.Model):
user = models.ForeignKey(User)
text = models.TextField()
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
这就是我在创建评论后创建操作的方式:
def comment_activity(sender, instance,created, **kwargs):
if created:
action.send(instance.user, verb='commented on', action_object=instance, target=instance.content_object)
post_save.connect(comment_activity, sender=Comment, dispatch_uid=comment_activity)
我也尝试使用action.send
信号将其保存在视图中,Action.objects.create()
但所有这些方法都失败了。
def create(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
comment = Comment.objects.create(**serializer.validated_data)
Action.objects.create(actor=comment.user,action_object=comment,verb="commented on",target=comment.content_object)
# action.send(request.user,action_object=comment,verb="commented on",target=comment.content_object)
return Response(
serializer.data, status=status.HTTP_201_CREATED
)
return Response({
'status': 'Bad request',
'message': 'Comment could not be created with received data.'
}, status=status.HTTP_400_BAD_REQUEST)
问题是否与内容类型的使用有关,如果确实如此,我该如何解决?
我尝试设置FETCH_RELATIONS
为 false ,但它不会改变任何东西。