I am trying to create a newsfeed using django activity stream. I am making a target stream but action object is not saved if it is a comment it does not save
"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"
}
}
When the action object is a photo it is saved properly. This my comment model:
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')
and this is how I create an action after the comment is created:
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)
I have also tried saving it in the views using the action.send
signals and Action.objects.create()
but all these approaches have failed.
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)
Does the problem have to do with the usage of content types and if it does how do I fix it?
I have tried setting FETCH_RELATIONS
to false but it doesn't change anything.