Stack Exchange
DRF serializer for ContentType object return null
I try to work with ContentTypes Framework and DRF. I have followed documentation, generic relationships
I have formed  BlogPost Model:
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)

and  Comment Model:
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')

I have formed 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',)
​    ​

When I check the api in the browser I get json answer
[
    {
        "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"
    }
]

How can I get the related comments. 
Thanks
ps: here is my view function 
class BlogPostListCreateAPIView(generics.ListCreateAPIView):
    queryset = BlogPost.objects.all()
    serializer_class = BlogPostSerializer

    def post(self, request, *args, **kwargs):
        title = request.data['title']
        self.slug = slugify(title)
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

    def perform_create(self, serializer):
        serializer.save()

class CommentListCreateAPIView(generics.ListCreateAPIView):
    queryset = BlogPost.objects.all()
    serializer_class = BlogPostSerializer

    def perform_create(self, serializer):
        blogpost_pk = self.kwargs.get("pk")
        blogpost = get_object_or_404(BlogPost, pk=pk)
        serializer.save()

class CommentDetailAPIView(generics.RetrieveUpdateDestroyAPIView):
    queryset = BlogPost.objects.all()
    serializer_class = BlogPostSerializer


Can you show the view function?

@funnydman I have added my view

Looks like you have switched the generic relation and reverse generic relation.  In the example you cited, the object being serialized is the TaggedItem, which is the model that has the generic foreign key.  But in your code you are trying to serialize a BlogPost object, which has the GenericRelation (i.e., the other side of the relationship).  From the documentation:

Note that reverse generic keys, expressed using the GenericRelation
  field, can be serialized using the regular relational field types,
  since the type of the target in the relationship is always known.

Try simply removing the CommentedObjectRelatedField line from your serializer.  Let the HyperlinkedModelSerializer handle the relationship.
Update
In a cursory local attempt, it did not work to leave the field out (in this case, I get an exception stating that "Object of type GenericRelatedObjectManager is not JSON serializable").  I had to explicitly add a HyperlinkedRelatedField in the serializer.

hmm.  just to double check: the BlogPost object with pk 1 does have some related comments, right?

I tried to check I can see them on admin panel, but I cant reach them from shell, I need some adjustments. my comment model has content_type and object_id. I have to pass those values when I  perform create.
