[当我尝试创建基本帖子时,它要求我传递有关作者的所有信息。
发布请求正文:
{
"author": {
"id": 1
},
"title" : "title1",
"content": "content1"
}
错误消息:
{
"author": {
"username": [
"This field is required."
],
"password": [
"This field is required."
],
"profile": [
"This field is required."
]
}
}
是否有可能像我在上面的示例中在POST上所尝试的那样仅通过其user id
分配作者,但是使用GET返回完整的author(user)对象?
#fixed OneToOneField to ForeignKey
class BasicPost(models.Model):
author = models.ForeignKey(User,null=True,on_delete=models.SET_NULL, related_name='author')
title = models.TextField()
content = models.TextField()
created_at = models.DateTimeField(default=timezone.now)
modified_at = models.DateTimeField(default=timezone.now)
您应该首先创建User对象,并获取User对象的ID,然后创建BasicPost对象,如下所示:
{
"author": 1,
"title": "title",
"content": "content1"
}