Django Rest Framework return True if relation exist
<p>I have two questions. How i can just return true if relation exist? For example I have post model and comment model also, comment have foreginKey to post. Now after post serialization i wanna have something like this </p>

<pre><code>{
id: 2
content: "My first post!"
has-comments: True
}
</code></pre>

<p>And my second question is how rename field name in model relation? Again we have post and comment. In comment model i have foregin key to post like </p>

<pre><code>post = models.ForeignKey(Post)
</code></pre>

<p>Now when i add new comment i send JSON data with {post: postIdHere}. Is possible to change post to postId only in drf not in django model?</p>

<p>I hope you understand me :)
Best Redgards,
Sierran.</p>

<p>The closest thing I can come up with is a custom <code>has_comments</code> field (rather than <code>has-comments</code>) with this in the serializer:</p>

<pre><code>from rest_framework import serializers

class YourSerializer(Either Serializer or ModelSerializer...):
    has_comments = serializers.SerializerMethodField()
    @staticmethod
    def get_has_comments(instance):
        # Choose whichever one works for you.
        # You did not specify some model names, so I am just making stuff up.
        return instance.post_set.exists()
        return Comment.objects.filter(post_id=instance.id).exists()
</code></pre>

<p>You may also have to specify the field in the serializer's <code>Meta</code> class. When first run, the framework will tell you exactly how.</p>

python django django-rest-framework