简体   繁体   English

DRF,使用选项获取外键对象

[英]DRF, get foreignkey objects using option

I'm trying to make backend using DRF, and I just faced a problem.我正在尝试使用 DRF 制作后端,但我遇到了一个问题。

models.py:模型.py:

class SchoolYear(models.Model):
    title = models.CharField(max_length=255, unique=True)

class Student(models.Model):
    name = models.CharField(max_length=10)
    school_year = models.ForeignKey(
        "students.SchoolYear",
        related_name="school_year",
        on_delete=models.CASCADE,
    )


class StudentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Student
        fields = '__all__'

Result from POSTMAN when using the option.使用该选项时来自 POSTMAN 的结果。


{
    "name": "Student Signup",
    "description": "",
    "renders": [
        "application/json",
        "text/html"
    ],
    "parses": [
        "application/json",
        "application/x-www-form-urlencoded",
        "multipart/form-data"
    ],
    "actions": {
        "POST": {
            "name": {
                "type": "string",
                "required": true,
                "read_only": false,
            },
            "school_year": {
                "type": "field",
                "required": true,
                "read_only": false,
            }
        }
    }
}

But I want to get a result like this.但我想得到这样的结果。 Because I have to deliver this to the front-end developer to make select form.因为我必须将其交付给前端开发人员以制作 select 表格。

I'd like to use this method, so I'd appreciate it if you could let me know if there's any other good method.我想用这个方法,所以如果你能告诉我是否还有其他好的方法,我将不胜感激。

{
    "name": "Student Signup",
    "description": "",
    "renders": [
        "application/json",
        "text/html"
    ],
    "parses": [
        "application/json",
        "application/x-www-form-urlencoded",
        "multipart/form-data"
    ],
    "actions": {
        "POST": {
            "name": {
                "type": "string",
                "required": true,
                "read_only": false,
            },
            "school_year": [
                    {
                        "id" : 1,
                        "title": "title1",
                    },
                    {
                        "id" : 2,
                        "title": "title2",
                    },
                    {
                        "id" : 3,
                        "title": "title3",
                    },
                    {
                        "id" : 4,
                        "title": "title4",
                    }
                ]
        }
    }
}

Try this:尝试这个:

class StudentSerializer(serializers.ModelSerializer):
    school_year_title = serializers.CharField(source='schoolyear.title')
    school_year_id = serializers.CharField(source='schoolyear.id')
    class Meta:
        model = Student
        fields = ('name','school_year_title','school_year_id'

This should do the trick.这应该可以解决问题。

You can take a look here:你可以在这里看看:

https://www.django-rest-framework.org/api-guide/fields/#source https://www.django-rest-framework.org/api-guide/fields/#source

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM