django序列化器嵌套_Django Rest Framework中用于OneToOne字段的序列化程序中的嵌套关​​系

本文介绍了如何在Django Rest Framework(DRF)中处理通过OneToOne字段引用另一个模型的序列化问题。通过使用嵌套关系,可以创建一个API,用于创建和更新用户对象,同时更新相关模型的属性,如学生专业等。
摘要由CSDN通过智能技术生成

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.modelsUser模型。 我想编写一个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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值