Array shows position ID instead of string value #8155
Answered
by
tomchristie
kruppy
asked this question in
Question & Answer
-
Hi, I have 2 models, Job and Topic: class Job(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4(), editable=False)
job_title = models.CharField(max_length=255)
topic = models.ManyToManyField(to=Topic, blank=True)
class Topic(models.Model):
name = models.CharField(max_length=255) When trying to expose it as an API via DRF, I get following JSON: [
{
"id": "fdca2bd2-b3b0-453b-a1a3-a52ca061e665",
"job_title": "Build MVP",
"topic": [
1,
2,
],
}
] But I want "topic" to contain the String-values instead of "1", "2". Do I have to do something in serializer? The jobs_serializer.py looks like that: class JobSerializer(serializers.ModelSerializer):
class Meta:
model = Job
fields = '__all__' Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Answered by
tomchristie
Sep 6, 2021
Replies: 1 comment 1 reply
-
The
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
kruppy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
SlugRelatedField
andStringRelatedField
examples in the docs are both close to what you're looking for.SlugRelatedField
can support a read-write field although you'd need name to be a unique model field if that's what you need, eg...name = models.CharField(max_length=255, unique=True)
StringRelatedField
is read-only, and would need you to define a__str__
method on theTopic
model.