django序列化器嵌套
The Django Rest Framework (DRF) is one of the effectively written frameworks around Django and helps build REST APIs for an application back-end.
Django Rest Framework( DRF )是围绕Django编写的有效框架之一,可帮助为应用程序后端构建REST API。
I was using it in one of my personal projects and stumbled upon this challenge of “serializing a model which is referencing another model via OneToOne field.”
我在自己的一个个人项目中使用了它,却偶然遇到了“串行化通过OneToOne字段引用另一个模型的模型”这一挑战。
`I was using the User model from django.contrib.auth.models
. I wanted to write an API to create and update a user object through a single API which also updates my model’s attributes. The solution was to use DRF’s Nested Relationships in serialization.
`我正在使用django.contrib.auth.models
的User模型。 我想编写一个API,以通过一个API创建和更新用户对象,该API还可以更新模型的属性。 解决方案是在序列化中使用DRF的嵌套关系 。
I shall assume you have a fair working knowledge of Python, virtualenv, pip, Django and DRF before proceeding. If not, please learn more and feel free to return if you are ever stuck on nested relationships in serialization.
在继续之前,我将假定您对Python,virtualenv,pip,Django和DRF具有一定的了解。 如果不是,请了解更多信息,如果您在序列化过程中陷入嵌套关系中,请随时返回。
The example I am considering here is a University Student model, referencing User model via the OneToOne field. My goal is a single API for