【发布时间】:2021-05-04 22:48:53
【问题描述】:
最近我遇到了嵌套对象发布的问题。在研究了涉及视图 create() 实现或用于读取和写入的单独序列化程序的正确解决方案之后,重写了 create() 方法。
我用 axios 发帖,我的对象是这样的:
{
comment: str
priority: str
file: int // -> this one is ForeignKey for another object and needs its PK which is ID
}
我希望我的帖子数据结构化的方式是:
{
comment: str
priority: str
file: str // -> which is file object name
}
发送此请求后,DRF 应在 db 中查找具有此名称的文件并将其 id 放在此文件中。
这是正确的方法吗?或者我应该嵌套整个对象而不是它的 id 吗?
另一个问题,offtopic:请求后首先调用什么:序列化程序或视图?
【问题讨论】:
标签: post django-rest-framework django-views axios django-serializer