我正在尝试将相关表的数据放入 Django Rest Framework 中的列表序列化程序中

Posted

技术标签:

【中文标题】我正在尝试将相关表的数据放入 Django Rest Framework 中的列表序列化程序中【英文标题】:I am trying to put a related table's data into a list serializer in Django Rest Framework 【发布时间】:2020-05-21 22:13:40 【问题描述】:

我有两个模型:

class Settings(models.Model):
    user = models.ForeignKey(
        User, on_delete=models.CASCADE)
    simplified = models.BooleanField()
    audio_download = models.BooleanField()
    show_read = models.BooleanField()
    show_pinyin = models.BooleanField()
    char_size = models.IntegerField()
    pinyin_size = models.IntegerField()
    start_date = models.DateField(null=True)
    end_date = models.DateField(null=True)

    class Meta:
        db_table = 'settings'


class HSK(models.Model):
    settings = models.OneToOneField(
        Settings, on_delete=models.CASCADE)
    hsk1 = models.BooleanField()
    hsk2 = models.BooleanField()
    hsk3 = models.BooleanField()
    hsk4 = models.BooleanField()
    hsk5 = models.BooleanField()
    hsk6 = models.BooleanField()
    hskplus = models.BooleanField()

我希望我的 GET 请求返回的 JSON 看起来像这样:


simplified: true,
audio_download: true,
...,
hsks: 
hsk1: true,
hsk2: true,
hsk3: false
 

我的 GET 请求如下所示:

def get(self, request, user_id):
    settings_saves = Settings.objects.select_related(
        "hsk").filter(user_id=user_id)
    serializer = SettingsSerializer(settings_saves, many=False)
    return Response("settings": serializer.data)

我的序列化器看起来像这样:

class SettingsSerializer(serializers.Serializer):
    user_id = serializers.IntegerField()
    simplified = serializers.BooleanField()
    audio_download = serializers.BooleanField()
    show_read = serializers.BooleanField()
    show_pinyin = serializers.BooleanField()
    char_size = serializers.IntegerField()
    pinyin_size = serializers.IntegerField()
    start_date = serializers.DateField()
    end_date = serializers.DateField()
    hsk1 = serializers.BooleanField(source="hsk.hsk1")
    hsk2 = serializers.BooleanField(source="hsk.hsk2")
    hsk3 = serializers.BooleanField(source="hsk.hsk3")
    hsk4 = serializers.BooleanField(source="hsk.hsk4")
    hsk5 = serializers.BooleanField(source="hsk.hsk5")
    hsk6 = serializers.BooleanField(source="hsk.hsk6")
    hskplus = serializers.BooleanField(source="hsk.hskplus")
    hsks = serializers.ListField(
        child=serializers.BooleanField(), source="hsk")

我可以单独获取所有字段:


    "settings": 
        "user_id": 1,
        "simplified": true,
        "audio_download": true,
        "show_read": true,
        "show_pinyin": true,
        "char_size": 1,
        "pinyin_size": 1,
        "start_date": "2020-05-06",
        "end_date": "2020-05-13",
        "hsk1": true,
        "hsk2": false,
        "hsk3": true,
        "hsk4": false,
        "hsk5": true,
        "hsk6": false,
        "hskplus": false
    

但这不是我想要的,我想要一个列表/数组中的所有 hsk 值。

如您在上面看到的,我已经查看了 ListField,但我不知道如何正确实现它,或者它是否是解决我的问题的正确方法。

如何在 Django 序列化程序中表示相关表中的数据列表?

【问题讨论】:

【参考方案1】:

您只需使用嵌套在父序列化程序中的序列化程序,如下所示:

class SettingsHSKSerializer(serializers.Serializer):
    settings_id = serializers.IntegerField()
    hsk1 = serializers.BooleanField()
    hsk2 = serializers.BooleanField()
    hsk3 = serializers.BooleanField()
    hsk4 = serializers.BooleanField()
    hsk5 = serializers.BooleanField()
    hsk6 = serializers.BooleanField()
    hskplus = serializers.BooleanField()


class SettingsSerializer(serializers.Serializer):
    user_id = serializers.IntegerField()
    simplified = serializers.BooleanField()
    audio_download = serializers.BooleanField()
    show_read = serializers.BooleanField()
    show_pinyin = serializers.BooleanField()
    char_size = serializers.IntegerField()
    pinyin_size = serializers.IntegerField()
    start_date = serializers.DateField()
    end_date = serializers.DateField()
    hsk = SettingsHSKSerializer()

【讨论】:

以上是关于我正在尝试将相关表的数据放入 Django Rest Framework 中的列表序列化程序中的主要内容,如果未能解决你的问题,请参考以下文章

将来自多个表的查询数据放入 html 表中

如何将链接放入 django 中的翻译标签

通过 Django 模型迁移覆盖表的风险?

带有 Contenttype 表的 Django 多数据库

Laravel Jetstream 比较两个表格以更改表格的背景

如何将特定用户下的数据放入 Django 中的模板中