DRF response list field
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
add a comment |
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
add a comment |
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
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
django-rest-framework response listfield
edited Nov 14 '18 at 12:08
kkr
asked Nov 14 '18 at 8:17
kkrkkr
307
307
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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