DRF response list field










1















How can I get response in an array format in Django drf?
I am new in python-drf programming. kindly help with solutions.



I just want the team_users in an array format, but now it is returning in an array with each letter seperated by quotes.





"id": 3,
"team_name": "team 3",
"team_icon": "/media/icon/underground_3Dz5sfD.png",
"team_users": [
"[",
"'",
"s",
"k",
"a",
"r",
"a",
"n",
"7",
"1",
"4",
"@",
"g",
"m",
"a",
"i",
"l",
".",
"c",
"o",
"m",
"'",
",",
" ",
"'",
"s",
"a",
"m",
"p",
"l",
"e",
"1",
"2",
"3",
"@",
"g",
"m",
"a",
"i",
"l",
".",
"c",
"o",
"m",
"'",
"]"
]
,



required format is :




 "id": 3,
"team_name": "team 3",
"team_icon": "/media/icon/underground_3Dz5sfD.png",
"team_users": ["first_email", "second_email"]



This is the code in serializers.py



class TeamSerializer(serializers.ModelSerializer):
team_users = serializers.ListField(child=serializers.EmailField(min_length=0, max_length=100))


class Meta():
model=Team
fields = ('id','team_name', 'team_icon', 'team_users')


models.py



class Team(models.Model):
team_name = models.CharField(max_length=255)
team_icon = models.ImageField()
team_users = models.EmailField()


In views.py



class GetTeam(APIView):


def get(self, request, *args, **kwargs):
data_list =
teams = models.Team.objects.all()
for team in teams:
data_list.append(serializers.TeamSerializer(team).data)
return Response(data_list)









share|improve this question




























    1















    How can I get response in an array format in Django drf?
    I am new in python-drf programming. kindly help with solutions.



    I just want the team_users in an array format, but now it is returning in an array with each letter seperated by quotes.





    "id": 3,
    "team_name": "team 3",
    "team_icon": "/media/icon/underground_3Dz5sfD.png",
    "team_users": [
    "[",
    "'",
    "s",
    "k",
    "a",
    "r",
    "a",
    "n",
    "7",
    "1",
    "4",
    "@",
    "g",
    "m",
    "a",
    "i",
    "l",
    ".",
    "c",
    "o",
    "m",
    "'",
    ",",
    " ",
    "'",
    "s",
    "a",
    "m",
    "p",
    "l",
    "e",
    "1",
    "2",
    "3",
    "@",
    "g",
    "m",
    "a",
    "i",
    "l",
    ".",
    "c",
    "o",
    "m",
    "'",
    "]"
    ]
    ,



    required format is :




     "id": 3,
    "team_name": "team 3",
    "team_icon": "/media/icon/underground_3Dz5sfD.png",
    "team_users": ["first_email", "second_email"]



    This is the code in serializers.py



    class TeamSerializer(serializers.ModelSerializer):
    team_users = serializers.ListField(child=serializers.EmailField(min_length=0, max_length=100))


    class Meta():
    model=Team
    fields = ('id','team_name', 'team_icon', 'team_users')


    models.py



    class Team(models.Model):
    team_name = models.CharField(max_length=255)
    team_icon = models.ImageField()
    team_users = models.EmailField()


    In views.py



    class GetTeam(APIView):


    def get(self, request, *args, **kwargs):
    data_list =
    teams = models.Team.objects.all()
    for team in teams:
    data_list.append(serializers.TeamSerializer(team).data)
    return Response(data_list)









    share|improve this question


























      1












      1








      1








      How can I get response in an array format in Django drf?
      I am new in python-drf programming. kindly help with solutions.



      I just want the team_users in an array format, but now it is returning in an array with each letter seperated by quotes.





      "id": 3,
      "team_name": "team 3",
      "team_icon": "/media/icon/underground_3Dz5sfD.png",
      "team_users": [
      "[",
      "'",
      "s",
      "k",
      "a",
      "r",
      "a",
      "n",
      "7",
      "1",
      "4",
      "@",
      "g",
      "m",
      "a",
      "i",
      "l",
      ".",
      "c",
      "o",
      "m",
      "'",
      ",",
      " ",
      "'",
      "s",
      "a",
      "m",
      "p",
      "l",
      "e",
      "1",
      "2",
      "3",
      "@",
      "g",
      "m",
      "a",
      "i",
      "l",
      ".",
      "c",
      "o",
      "m",
      "'",
      "]"
      ]
      ,



      required format is :




       "id": 3,
      "team_name": "team 3",
      "team_icon": "/media/icon/underground_3Dz5sfD.png",
      "team_users": ["first_email", "second_email"]



      This is the code in serializers.py



      class TeamSerializer(serializers.ModelSerializer):
      team_users = serializers.ListField(child=serializers.EmailField(min_length=0, max_length=100))


      class Meta():
      model=Team
      fields = ('id','team_name', 'team_icon', 'team_users')


      models.py



      class Team(models.Model):
      team_name = models.CharField(max_length=255)
      team_icon = models.ImageField()
      team_users = models.EmailField()


      In views.py



      class GetTeam(APIView):


      def get(self, request, *args, **kwargs):
      data_list =
      teams = models.Team.objects.all()
      for team in teams:
      data_list.append(serializers.TeamSerializer(team).data)
      return Response(data_list)









      share|improve this question
















      How can I get response in an array format in Django drf?
      I am new in python-drf programming. kindly help with solutions.



      I just want the team_users in an array format, but now it is returning in an array with each letter seperated by quotes.





      "id": 3,
      "team_name": "team 3",
      "team_icon": "/media/icon/underground_3Dz5sfD.png",
      "team_users": [
      "[",
      "'",
      "s",
      "k",
      "a",
      "r",
      "a",
      "n",
      "7",
      "1",
      "4",
      "@",
      "g",
      "m",
      "a",
      "i",
      "l",
      ".",
      "c",
      "o",
      "m",
      "'",
      ",",
      " ",
      "'",
      "s",
      "a",
      "m",
      "p",
      "l",
      "e",
      "1",
      "2",
      "3",
      "@",
      "g",
      "m",
      "a",
      "i",
      "l",
      ".",
      "c",
      "o",
      "m",
      "'",
      "]"
      ]
      ,



      required format is :




       "id": 3,
      "team_name": "team 3",
      "team_icon": "/media/icon/underground_3Dz5sfD.png",
      "team_users": ["first_email", "second_email"]



      This is the code in serializers.py



      class TeamSerializer(serializers.ModelSerializer):
      team_users = serializers.ListField(child=serializers.EmailField(min_length=0, max_length=100))


      class Meta():
      model=Team
      fields = ('id','team_name', 'team_icon', 'team_users')


      models.py



      class Team(models.Model):
      team_name = models.CharField(max_length=255)
      team_icon = models.ImageField()
      team_users = models.EmailField()


      In views.py



      class GetTeam(APIView):


      def get(self, request, *args, **kwargs):
      data_list =
      teams = models.Team.objects.all()
      for team in teams:
      data_list.append(serializers.TeamSerializer(team).data)
      return Response(data_list)






      django-rest-framework response listfield






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 12:08







      kkr

















      asked Nov 14 '18 at 8:17









      kkrkkr

      307




      307






















          1 Answer
          1






          active

          oldest

          votes


















          1














          One of the possible solution is to use 'SerializerMethodField'



          class TeamSerializer(serializers.ModelSerializer):

          team_users = serializers.SerializerMethodField(read_only=True)

          def get_team_users(self, obj):
          return [x.email for x in obj.team_users]


          Also depends on your team user model, but you can tailor this to your needs.






          share|improve this answer























          • Got this as error AttributeError 'str' object has no attribute 'email'

            – kkr
            Nov 14 '18 at 11:25











          • That's what I meant, you need to add your models to your question. Without that I only assumed that team user is a model with email attribute.

            – Enthusiast Martin
            Nov 14 '18 at 11:38











          • edited the question.

            – kkr
            Nov 14 '18 at 11:50











          • can you please help me with the problem, added the models and views

            – kkr
            Nov 14 '18 at 12:16











          • you model design is not probably what you want, and it is out of scope to do here on SO. You probably want a teamuser to be a model and add relation to the team model so you can multiple users in a team.

            – Enthusiast Martin
            Nov 15 '18 at 10:54










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295675%2fdrf-response-list-field%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          One of the possible solution is to use 'SerializerMethodField'



          class TeamSerializer(serializers.ModelSerializer):

          team_users = serializers.SerializerMethodField(read_only=True)

          def get_team_users(self, obj):
          return [x.email for x in obj.team_users]


          Also depends on your team user model, but you can tailor this to your needs.






          share|improve this answer























          • Got this as error AttributeError 'str' object has no attribute 'email'

            – kkr
            Nov 14 '18 at 11:25











          • That's what I meant, you need to add your models to your question. Without that I only assumed that team user is a model with email attribute.

            – Enthusiast Martin
            Nov 14 '18 at 11:38











          • edited the question.

            – kkr
            Nov 14 '18 at 11:50











          • can you please help me with the problem, added the models and views

            – kkr
            Nov 14 '18 at 12:16











          • you model design is not probably what you want, and it is out of scope to do here on SO. You probably want a teamuser to be a model and add relation to the team model so you can multiple users in a team.

            – Enthusiast Martin
            Nov 15 '18 at 10:54















          1














          One of the possible solution is to use 'SerializerMethodField'



          class TeamSerializer(serializers.ModelSerializer):

          team_users = serializers.SerializerMethodField(read_only=True)

          def get_team_users(self, obj):
          return [x.email for x in obj.team_users]


          Also depends on your team user model, but you can tailor this to your needs.






          share|improve this answer























          • Got this as error AttributeError 'str' object has no attribute 'email'

            – kkr
            Nov 14 '18 at 11:25











          • That's what I meant, you need to add your models to your question. Without that I only assumed that team user is a model with email attribute.

            – Enthusiast Martin
            Nov 14 '18 at 11:38











          • edited the question.

            – kkr
            Nov 14 '18 at 11:50











          • can you please help me with the problem, added the models and views

            – kkr
            Nov 14 '18 at 12:16











          • you model design is not probably what you want, and it is out of scope to do here on SO. You probably want a teamuser to be a model and add relation to the team model so you can multiple users in a team.

            – Enthusiast Martin
            Nov 15 '18 at 10:54













          1












          1








          1







          One of the possible solution is to use 'SerializerMethodField'



          class TeamSerializer(serializers.ModelSerializer):

          team_users = serializers.SerializerMethodField(read_only=True)

          def get_team_users(self, obj):
          return [x.email for x in obj.team_users]


          Also depends on your team user model, but you can tailor this to your needs.






          share|improve this answer













          One of the possible solution is to use 'SerializerMethodField'



          class TeamSerializer(serializers.ModelSerializer):

          team_users = serializers.SerializerMethodField(read_only=True)

          def get_team_users(self, obj):
          return [x.email for x in obj.team_users]


          Also depends on your team user model, but you can tailor this to your needs.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 11:13









          Enthusiast MartinEnthusiast Martin

          1,3202313




          1,3202313












          • Got this as error AttributeError 'str' object has no attribute 'email'

            – kkr
            Nov 14 '18 at 11:25











          • That's what I meant, you need to add your models to your question. Without that I only assumed that team user is a model with email attribute.

            – Enthusiast Martin
            Nov 14 '18 at 11:38











          • edited the question.

            – kkr
            Nov 14 '18 at 11:50











          • can you please help me with the problem, added the models and views

            – kkr
            Nov 14 '18 at 12:16











          • you model design is not probably what you want, and it is out of scope to do here on SO. You probably want a teamuser to be a model and add relation to the team model so you can multiple users in a team.

            – Enthusiast Martin
            Nov 15 '18 at 10:54

















          • Got this as error AttributeError 'str' object has no attribute 'email'

            – kkr
            Nov 14 '18 at 11:25











          • That's what I meant, you need to add your models to your question. Without that I only assumed that team user is a model with email attribute.

            – Enthusiast Martin
            Nov 14 '18 at 11:38











          • edited the question.

            – kkr
            Nov 14 '18 at 11:50











          • can you please help me with the problem, added the models and views

            – kkr
            Nov 14 '18 at 12:16











          • you model design is not probably what you want, and it is out of scope to do here on SO. You probably want a teamuser to be a model and add relation to the team model so you can multiple users in a team.

            – Enthusiast Martin
            Nov 15 '18 at 10:54
















          Got this as error AttributeError 'str' object has no attribute 'email'

          – kkr
          Nov 14 '18 at 11:25





          Got this as error AttributeError 'str' object has no attribute 'email'

          – kkr
          Nov 14 '18 at 11:25













          That's what I meant, you need to add your models to your question. Without that I only assumed that team user is a model with email attribute.

          – Enthusiast Martin
          Nov 14 '18 at 11:38





          That's what I meant, you need to add your models to your question. Without that I only assumed that team user is a model with email attribute.

          – Enthusiast Martin
          Nov 14 '18 at 11:38













          edited the question.

          – kkr
          Nov 14 '18 at 11:50





          edited the question.

          – kkr
          Nov 14 '18 at 11:50













          can you please help me with the problem, added the models and views

          – kkr
          Nov 14 '18 at 12:16





          can you please help me with the problem, added the models and views

          – kkr
          Nov 14 '18 at 12:16













          you model design is not probably what you want, and it is out of scope to do here on SO. You probably want a teamuser to be a model and add relation to the team model so you can multiple users in a team.

          – Enthusiast Martin
          Nov 15 '18 at 10:54





          you model design is not probably what you want, and it is out of scope to do here on SO. You probably want a teamuser to be a model and add relation to the team model so you can multiple users in a team.

          – Enthusiast Martin
          Nov 15 '18 at 10:54



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295675%2fdrf-response-list-field%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          What does pagestruct do in Eviews?

          Dutch intervention in Lombok and Karangasem

          Channel Islands