How to get the value of the key in the serializer in manytomanyfield in Django Rest Framework? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Django foreign key access in save() functionHow to get the current time in PythonHow do I sort a dictionary by value?Show information of subclass in list_display djangoProducts catalogue: filter by parametersDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to expose some specific fields of model_b based on a field of model_a?How to set dynamic initial values to django modelform fieldHow to define Mode with generic ForeignKey in DjangoHow to check if Django Signal works?

Unix AIX passing variable and arguments to expect and spawn

Determine the generator of an ideal of ring of integers

lm and glm function in R

Is the Mordenkainen's Sword spell underpowered?

Marquee sign letters

Kepler's 3rd law: ratios don't fit data

Is my guitar’s action too high?

Why does BitLocker not use RSA?

Why isn't everyone flabbergasted about Bran's "gift"?

Why is one lightbulb in a string illuminated?

Can the van der Waals coefficients be negative in the van der Waals equation for real gases?

What is the evidence that custom checks in Northern Ireland are going to result in violence?

How do I deal with an erroneously large refund?

How to make an animal which can only breed for a certain number of generations?

Does Prince Arnaud cause someone holding the Princess to lose?

When speaking, how do you change your mind mid-sentence?

How was Lagrange appointed professor of mathematics so early?

Does the universe have a fixed centre of mass?

Why are two-digit numbers in Jonathan Swift's "Gulliver's Travels" (1726) written in "German style"?

Why aren't road bike wheels tiny?

Married in secret, can marital status in passport be changed at a later date?

Weaponising the Grasp-at-a-Distance spell

Does traveling In The United States require a passport or can I use my green card if not a US citizen?

What came first? Venom as the movie or as the song?



How to get the value of the key in the serializer in manytomanyfield in Django Rest Framework?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Django foreign key access in save() functionHow to get the current time in PythonHow do I sort a dictionary by value?Show information of subclass in list_display djangoProducts catalogue: filter by parametersDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to expose some specific fields of model_b based on a field of model_a?How to set dynamic initial values to django modelform fieldHow to define Mode with generic ForeignKey in DjangoHow to check if Django Signal works?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm having a problem with displaying the data during serialization.



This is my model:



from django.db import models

class Paradigmn(models.Model):
name = models.CharField(max_length=50)

def __str__(self):
return self.name

class Language(models.Model):
name = models.CharField(max_length=50)
paradigm = models.ForeignKey(Paradigmn, on_delete=models.CASCADE)

def __str__(self):
return self.name

class Programmer(models.Model):
name = models.CharField(max_length=50)
languages = models.ManyToManyField(Language, related_name='languages')

def __str__(self):
return self.name


And this is my serializer:



from languages.models import Language, Paradigmn, Programmer

class LanguageSerializer(serializers.ModelSerializer):
paradigms = serializers.ReadOnlyField(source='paradigm.name')

class Meta:
model = Language
fields = ('id', 'name', 'paradigms')

class ParadigmnSerializer(serializers.ModelSerializer):

class Meta:
model = Paradigmn
fields = ('id', 'name',)

class ProgrammerSerializer(serializers.ModelSerializer):
languages = LanguageSerializer(many=True, read_only=True)

class Meta:
model = Programmer
fields = ('id', 'name', 'languages')


And this is the result:



[

"id": 1,
"name": "Ryan",
"languages": [

"id": 1,
"name": "Java",
"paradigms": "Object-Oriented"

]
,

"id": 2,
"name": "Jean",
"languages": [

"id": 3,
"name": "Python",
"paradigms": "Object-Oriented"

]
,

"id": 3,
"name": "Michael",
"languages": [

"id": 2,
"name": "Elixir",
"paradigms": "Functional"

]



I just want to show on the languages array, the name of the language instead of the all the details of the language array. What is the best solution for this?










share|improve this question
























  • No need to have ` languages = LanguageSerializer(many=True, read_only=True)` in LanguageSerializer` than. As self.name properly define in Language Model.

    – Shakil
    Mar 9 at 2:50

















1















I'm having a problem with displaying the data during serialization.



This is my model:



from django.db import models

class Paradigmn(models.Model):
name = models.CharField(max_length=50)

def __str__(self):
return self.name

class Language(models.Model):
name = models.CharField(max_length=50)
paradigm = models.ForeignKey(Paradigmn, on_delete=models.CASCADE)

def __str__(self):
return self.name

class Programmer(models.Model):
name = models.CharField(max_length=50)
languages = models.ManyToManyField(Language, related_name='languages')

def __str__(self):
return self.name


And this is my serializer:



from languages.models import Language, Paradigmn, Programmer

class LanguageSerializer(serializers.ModelSerializer):
paradigms = serializers.ReadOnlyField(source='paradigm.name')

class Meta:
model = Language
fields = ('id', 'name', 'paradigms')

class ParadigmnSerializer(serializers.ModelSerializer):

class Meta:
model = Paradigmn
fields = ('id', 'name',)

class ProgrammerSerializer(serializers.ModelSerializer):
languages = LanguageSerializer(many=True, read_only=True)

class Meta:
model = Programmer
fields = ('id', 'name', 'languages')


And this is the result:



[

"id": 1,
"name": "Ryan",
"languages": [

"id": 1,
"name": "Java",
"paradigms": "Object-Oriented"

]
,

"id": 2,
"name": "Jean",
"languages": [

"id": 3,
"name": "Python",
"paradigms": "Object-Oriented"

]
,

"id": 3,
"name": "Michael",
"languages": [

"id": 2,
"name": "Elixir",
"paradigms": "Functional"

]



I just want to show on the languages array, the name of the language instead of the all the details of the language array. What is the best solution for this?










share|improve this question
























  • No need to have ` languages = LanguageSerializer(many=True, read_only=True)` in LanguageSerializer` than. As self.name properly define in Language Model.

    – Shakil
    Mar 9 at 2:50













1












1








1








I'm having a problem with displaying the data during serialization.



This is my model:



from django.db import models

class Paradigmn(models.Model):
name = models.CharField(max_length=50)

def __str__(self):
return self.name

class Language(models.Model):
name = models.CharField(max_length=50)
paradigm = models.ForeignKey(Paradigmn, on_delete=models.CASCADE)

def __str__(self):
return self.name

class Programmer(models.Model):
name = models.CharField(max_length=50)
languages = models.ManyToManyField(Language, related_name='languages')

def __str__(self):
return self.name


And this is my serializer:



from languages.models import Language, Paradigmn, Programmer

class LanguageSerializer(serializers.ModelSerializer):
paradigms = serializers.ReadOnlyField(source='paradigm.name')

class Meta:
model = Language
fields = ('id', 'name', 'paradigms')

class ParadigmnSerializer(serializers.ModelSerializer):

class Meta:
model = Paradigmn
fields = ('id', 'name',)

class ProgrammerSerializer(serializers.ModelSerializer):
languages = LanguageSerializer(many=True, read_only=True)

class Meta:
model = Programmer
fields = ('id', 'name', 'languages')


And this is the result:



[

"id": 1,
"name": "Ryan",
"languages": [

"id": 1,
"name": "Java",
"paradigms": "Object-Oriented"

]
,

"id": 2,
"name": "Jean",
"languages": [

"id": 3,
"name": "Python",
"paradigms": "Object-Oriented"

]
,

"id": 3,
"name": "Michael",
"languages": [

"id": 2,
"name": "Elixir",
"paradigms": "Functional"

]



I just want to show on the languages array, the name of the language instead of the all the details of the language array. What is the best solution for this?










share|improve this question
















I'm having a problem with displaying the data during serialization.



This is my model:



from django.db import models

class Paradigmn(models.Model):
name = models.CharField(max_length=50)

def __str__(self):
return self.name

class Language(models.Model):
name = models.CharField(max_length=50)
paradigm = models.ForeignKey(Paradigmn, on_delete=models.CASCADE)

def __str__(self):
return self.name

class Programmer(models.Model):
name = models.CharField(max_length=50)
languages = models.ManyToManyField(Language, related_name='languages')

def __str__(self):
return self.name


And this is my serializer:



from languages.models import Language, Paradigmn, Programmer

class LanguageSerializer(serializers.ModelSerializer):
paradigms = serializers.ReadOnlyField(source='paradigm.name')

class Meta:
model = Language
fields = ('id', 'name', 'paradigms')

class ParadigmnSerializer(serializers.ModelSerializer):

class Meta:
model = Paradigmn
fields = ('id', 'name',)

class ProgrammerSerializer(serializers.ModelSerializer):
languages = LanguageSerializer(many=True, read_only=True)

class Meta:
model = Programmer
fields = ('id', 'name', 'languages')


And this is the result:



[

"id": 1,
"name": "Ryan",
"languages": [

"id": 1,
"name": "Java",
"paradigms": "Object-Oriented"

]
,

"id": 2,
"name": "Jean",
"languages": [

"id": 3,
"name": "Python",
"paradigms": "Object-Oriented"

]
,

"id": 3,
"name": "Michael",
"languages": [

"id": 2,
"name": "Elixir",
"paradigms": "Functional"

]



I just want to show on the languages array, the name of the language instead of the all the details of the language array. What is the best solution for this?







python django django-rest-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 2:46









Pikachu the Purple Wizard

2,12461529




2,12461529










asked Mar 9 at 2:41









Ershin BotErshin Bot

162




162












  • No need to have ` languages = LanguageSerializer(many=True, read_only=True)` in LanguageSerializer` than. As self.name properly define in Language Model.

    – Shakil
    Mar 9 at 2:50

















  • No need to have ` languages = LanguageSerializer(many=True, read_only=True)` in LanguageSerializer` than. As self.name properly define in Language Model.

    – Shakil
    Mar 9 at 2:50
















No need to have ` languages = LanguageSerializer(many=True, read_only=True)` in LanguageSerializer` than. As self.name properly define in Language Model.

– Shakil
Mar 9 at 2:50





No need to have ` languages = LanguageSerializer(many=True, read_only=True)` in LanguageSerializer` than. As self.name properly define in Language Model.

– Shakil
Mar 9 at 2:50












1 Answer
1






active

oldest

votes


















0














One solution:



from rest_framework.serializers import SerializerMethodField

class ProgrammerSerializer(serializers.ModelSerializer):
languagelist = SerializerMethodField()

def get_languagelist(self, obj):
return ['name': i.name for i in obj.languages.all()]

class Meta:
model = Programmer
fields = ('id', 'name', 'languagelist')





share|improve this answer























  • The solution is not working. It only returns: [ 1, 3, 2 ]

    – Ershin Bot
    Mar 11 at 9:15











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%2f55073500%2fhow-to-get-the-value-of-the-key-in-the-serializer-in-manytomanyfield-in-django-r%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









0














One solution:



from rest_framework.serializers import SerializerMethodField

class ProgrammerSerializer(serializers.ModelSerializer):
languagelist = SerializerMethodField()

def get_languagelist(self, obj):
return ['name': i.name for i in obj.languages.all()]

class Meta:
model = Programmer
fields = ('id', 'name', 'languagelist')





share|improve this answer























  • The solution is not working. It only returns: [ 1, 3, 2 ]

    – Ershin Bot
    Mar 11 at 9:15















0














One solution:



from rest_framework.serializers import SerializerMethodField

class ProgrammerSerializer(serializers.ModelSerializer):
languagelist = SerializerMethodField()

def get_languagelist(self, obj):
return ['name': i.name for i in obj.languages.all()]

class Meta:
model = Programmer
fields = ('id', 'name', 'languagelist')





share|improve this answer























  • The solution is not working. It only returns: [ 1, 3, 2 ]

    – Ershin Bot
    Mar 11 at 9:15













0












0








0







One solution:



from rest_framework.serializers import SerializerMethodField

class ProgrammerSerializer(serializers.ModelSerializer):
languagelist = SerializerMethodField()

def get_languagelist(self, obj):
return ['name': i.name for i in obj.languages.all()]

class Meta:
model = Programmer
fields = ('id', 'name', 'languagelist')





share|improve this answer













One solution:



from rest_framework.serializers import SerializerMethodField

class ProgrammerSerializer(serializers.ModelSerializer):
languagelist = SerializerMethodField()

def get_languagelist(self, obj):
return ['name': i.name for i in obj.languages.all()]

class Meta:
model = Programmer
fields = ('id', 'name', 'languagelist')






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 9 at 3:52









Waket ZhengWaket Zheng

79339




79339












  • The solution is not working. It only returns: [ 1, 3, 2 ]

    – Ershin Bot
    Mar 11 at 9:15

















  • The solution is not working. It only returns: [ 1, 3, 2 ]

    – Ershin Bot
    Mar 11 at 9:15
















The solution is not working. It only returns: [ 1, 3, 2 ]

– Ershin Bot
Mar 11 at 9:15





The solution is not working. It only returns: [ 1, 3, 2 ]

– Ershin Bot
Mar 11 at 9:15



















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%2f55073500%2fhow-to-get-the-value-of-the-key-in-the-serializer-in-manytomanyfield-in-django-r%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







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко