Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django ModelSerializer中从请求中检索其他JSON数据_Python_Json_Django_Django Rest Framework_Serialization - Fatal编程技术网

Python 如何在django ModelSerializer中从请求中检索其他JSON数据

Python 如何在django ModelSerializer中从请求中检索其他JSON数据,python,json,django,django-rest-framework,serialization,Python,Json,Django,Django Rest Framework,Serialization,我有一个模型序列化程序,它创建一个程序对象以及另一个OutboundProgram对象。我收到的json包含程序对象的所有细节,以及创建OutboundProgram所需的其他细节。由于序列化程序只读取特定对象的字段,因此如何检索其他字段。另外,我们的团队负责人不希望我使用嵌套的JSON 请求接收到的json: { "linkage": "AP", "name": "something", "academic_year": 2017, "terms_available":[1,2], "is_g

我有一个模型序列化程序,它创建一个程序对象以及另一个OutboundProgram对象。我收到的json包含程序对象的所有细节,以及创建OutboundProgram所需的其他细节。由于序列化程序只读取特定对象的字段,因此如何检索其他字段。另外,我们的团队负责人不希望我使用嵌套的JSON

请求接收到的json:

{
"linkage": "AP",
"name": "something",
"academic_year": 2017,
"terms_available":[1,2],
"is_graduate": false,
"requirement_deadline":"2011-10-16",
"institution": 3
}
models.py

class Program(SoftDeletionModel):
    linkage = ForeignKey(Linkage)
    name = CharField(max_length=64)
    academic_year = ForeignKey(AcademicYear)
    terms_available = ManyToManyField(Term)
    is_graduate = BooleanField()

    def __str__(self):
        return self.name


class OutboundProgram(SoftDeletionModel):
    program = ForeignKey(Program)
    requirement_deadline = DateField()
    institution = ForeignKey(Institution)
序列化程序.py

class OutboundProgramSerializer(ModelSerializer):
    class Meta:
        model = Program
        fields = "__all__"

    def create(self, validated_data):
        terms = validated_data.pop('terms_available')
        program = Program.objects.create(**validated_data)
        for term in terms:
            program.terms_available.add(term)
        program.save()

        outbound_program = OutboundProgram.objects.create(program=program)
        #how to assign outbound_program.requirements_deadline from json
        return program
任何形式的帮助都是有用的。谢谢

尝试使用:

尝试使用:


序列化程序.py


序列化程序.py


我不认为这会起作用验证数据不包括2个额外字段它只会返回一个键错误我不认为这会起作用验证数据不包括2个额外字段它只会返回一个键错误很高兴帮助你!很高兴帮助你!
request = self.context['request']
terms = request.data.get('terms_available', [])
class OutboundProgramSerializer(ModelSerializer):
    requirement_deadline = serializers.DateField(write_only=True)
    institution = serializers.IntegerField(write_only=True)
    class Meta:
        model = Program
        fields = "__all__"

    def create(self, validated_data):
        terms = validated_data.pop('terms_available')
        program = Program.objects.create(**validated_data)
        for term in terms:
            program.terms_available.add(term)
        program.save()

        outbound_program = OutboundProgram.objects.create(program=program)
        outbound_program.requirement_deadline = validated_data.pop('requirement_deadline')
        outbound_program.institution_id = validated_data.pop('institution')
        outbound_program.save()
        return program