Django Rest Api-ManyToManyField,在“练习"数组中显示"title"而不是"id" [英] Django Rest Api - ManyToManyField, Display 'title' instead of 'id' in the Exercises Array

查看:95
本文介绍了Django Rest Api-ManyToManyField,在“练习"数组中显示"title"而不是"id"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django Rest Api-ManyToManyField,在练习"数组中显示"title"而不是"id"

Django Rest Api - ManyToManyField, Display 'title' instead of 'id' in the Exercises Array

HTTP 200 OK
Allow: GET, POST, PUT, DELETE, PATCH
Content-Type: application/json
Vary: Accept

[
{
    "id": 1,
    "title": "Push Workout Bjarred",
    "description": "Kör Hårt!",
    "exercises": [
        3,
        4,
        5,
        6,
        9,
        10
    ],
    "cardio": [
        4
    ]
},
{
    "id": 2,
    "title": "Pull Workout Loddekopinge",
    "description": "",
    "exercises": [
        1,
        2,
        7,
        8
    ],
    "cardio": []
},
{
    "id": 3,
    "title": "CardioPass",
    "description": "",
    "exercises": [],
    "cardio": [
        2,
        3,
        4
    ]
}
]

序列化器(所以我想显示每个练习的标题而非ID)

Serializer (So I want to display the title and not the id for every exercise)

class WorkoutSerializer(serializers.ModelSerializer):
    class Meta:
        model = Workout
        fields = ('id', 'title', 'description', 'exercises', 'cardio')

这里是模型,请注意,我想在api数组中显示每个练习的练习名称,我不需要id! -现在通过了! :)

Here are the models, note that I want to display the exercise name for every exercise in the api array, I don't want the id! - That is passed right now! :)

class Bodypart(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

class Exercise(models.Model):
    name = models.CharField(max_length=40)
    bodyparts = models.ManyToManyField(Bodypart, blank=True)

    def __str__(self):
        return self.name

class Cardio(models.Model):
    name = models.CharField(max_length=40)
    time = models.IntegerField(default=10)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'cardio'

class Workout(models.Model):
    title = models.CharField(max_length=120)
    description = models.CharField(max_length=1000, blank=True)
    exercises = models.ManyToManyField(Exercise, blank=True)
    cardio = models.ManyToManyField(Cardio, blank=True)

    def __str__(self):
        return self.title

推荐答案

您可以通过更改您的WorkoutSerializer来获得此

You can get this by changing your WorkoutSerializer like this

class WorkoutSerializer(serializers.ModelSerializer):
    exercises = serializers.StringRelatedField(many=True, read_only=True)
    class Meta:
        fields = ('id', 'title', 'description', 'exercises', 'cardio', )

您将获得像这样的结果json

You will get the result json like this

[
{
    "id": 1,
    "title": "Push Workout Bjarred",
    "description": "Kör Hårt!",
    "exercises": [
        "Exercise 1",
        "Exercise 2",
        "Exercise 4",
        "Exercise 3"
    ],
    "cardio": [
        4
    ]
},
...

有关django rest序列化程序的更多信息,请参见 https://www.django- rest-framework.org/api-guide/relations/

More about django rest serializers see https://www.django-rest-framework.org/api-guide/relations/

这篇关于Django Rest Api-ManyToManyField,在“练习"数组中显示"title"而不是"id"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆