0

i have a Community app with a field of subscriber related to the accounts app but in the response it return counts instead of using account serializer

CommunitySerializer:

class CommunitySerializer(serializers.ModelSerializer):

    class Meta:
        model = Community
        fields = ('name', 'about', 'subscribers', 'moderators')

AccountsSerializer:

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('username',)

It Return:

{
"name": "pcmasterrace",
"about": "Welcome to the official subreddit of the PC Master Race. In this subreddit, we celebrate and promote the ultimate gaming and working platform. Ascend to a level that respects your eyes, your wallet, your mind, and your heart. Ascend to... the PC Master Race.",
"community_rules": [
    {
        "rule": "Rule 1 - Off Topic",
        "description": "DOnt fuck"
    },
    {
        "rule": "Rule 2 - No tech support",
        "description": "community_rulescommunity_rulescommunity_rulescommunity_rulescommunity_rules"
    }
],
"subscribers": [
    1
],
"moderators": [
    1
]
}
5
  • How does it return community_rules if that is not within the serializer? Commented Mar 11, 2019 at 19:31
  • i delete it for the question Commented Mar 11, 2019 at 19:34
  • Is it actually passing back a count or is it a list of ForeignKey primary keys? Commented Mar 11, 2019 at 19:39
  • its passing back a count of subscriber users Commented Mar 11, 2019 at 19:41
  • how is the subscribers field defined in Community model? Commented Mar 11, 2019 at 22:13

1 Answer 1

1

Instead of count, could 1 in

"subscribers": [
    1
],

be the id of the only subscriber? Either way, if you want to get subscribers as a list of UserSerializer instances, you need to define it explicitly in the serializer, otherwise DRF would use ids by default.

class CommunitySerializer(serializers.ModelSerializer):
    subscribers = UserSerializer(many=True)
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.