Rendering number of hits on a page in an API

Viewed 56

I'm trying to render the number of pages my detailview gets with django-hitcount on my API. That means I need a field representing it in my serializers.py. I don't know how right this is, but adding hit_count_generic to my list of fields on in my serializers throws back the error:

Object of type 'GenericRelatedObjectManager' is not JSON serializable

I see the above when I try viewing on my browser. What field am I meant to use in representing the number of hits?

This is what my Post model looks like:

class Post(models.Model, HitCountMixin):
    author = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
    title = models.CharField(max_length=350, unique=True)
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)
    view_count = models.IntegerField(default=0)
    published_date = models.DateTimeField(blank=True, null=True)
    featured_image = models.FileField(upload_to='images/')
    tags = TaggableManager()
    hit_count_generic = GenericRelation(
        HitCount, object_id_field='object_pk',
        related_query_name='hit_count_generic_relation')

This is what my serializer class looks like:

class PostSerializer(serializers.ModelSerializer):
    created_date = fields.DateTimeField(input_formats=['%Y-%m-%d:%H:%M:%S'])
    published_date = fields.DateTimeField(input_formats=['%Y-%m-%d:%H:%M:%S'])

    class Meta:
        fields = ('id', 'author', 'title', 'text', 'featured_image', 'created_date', 'published_date', hit_count_generic)
        model = Post

With hit_count_generic as a field, it produces that error above.

0 Answers
Related