如何使用 TastyPie 在模型具有一对多自引用关系时创建记录?
How to create records when a model has one-to-many self referential relationship using TastyPie?
我正在使用 TastyPie 发出 POST 请求。 Task 模型通过 parent_task_id 字段具有一对多的自引用关系。
型号:
class Task(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
parent_task_id = models.ForeignKey(
"self",
on_delete=models.CASCADE,
null=True, blank=True)
在我的api.py
class TaskResource(ModelResource):
parent_task_id_id = fields.ToOneField('self', 'id', null=True, full=True)
class Meta:
queryset = Task.objects.all()
authorization = Authorization()
allowed_methods = ['post']
resource_name = "create_task"
当我使用 Postman 指定 parent_task_id 时,我无法创建任务。
{
"title": "ABCDERT",
"description": "world this week",
"due_date": "2018-11-12 1:2:1",
"parent_task_id_id": "2"
}
这是我在执行此操作时收到的错误消息:
"error_message": "An incorrect URL was provided '2' for the 'CreateTaskResource' resource.",
您应该指定 parent_task 的 uri
而不是 id
,例如
{
"title": "ABCDERT",
"description": "world this week",
"due_date": "2018-11-12 1:2:1",
"parent_task_id_id": "/create_task/2"
}
另外,这样定义资源中的外键字段是不正确的,
class TaskResource(ModelResource):
parent_task_id_id = fields.ToOneField('self', 'id', null=True, full=True)
详情可以看docs
我调整你的例子,比如:
型号:
class Task(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
parent_task = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True)
api.py
class TaskResource(ModelResource):
parent_task = fields.ToOneField('self', 'parent_task', null=True,
full=True)
class Meta:
queryset = Task.objects.all()
authorization = Authorization()
allowed_methods = ['post', 'get']
filtering = {'id': ALL, 'parent_task': ALL_WITH_RELATIONS}
resource_name = "task"
- 创建任务
POST身材喜欢:
{
"title": "task2",
"description": "world this week",
"due_date": "2018-11-12 1:2:1",
"parent_task":"/api/v1/task/1/"
}
- 外键查询
GET 参数如:
0.0.0.0:8000/api/v1/task/?parent_task__id=1
我正在使用 TastyPie 发出 POST 请求。 Task 模型通过 parent_task_id 字段具有一对多的自引用关系。
型号:
class Task(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
parent_task_id = models.ForeignKey(
"self",
on_delete=models.CASCADE,
null=True, blank=True)
在我的api.py
class TaskResource(ModelResource):
parent_task_id_id = fields.ToOneField('self', 'id', null=True, full=True)
class Meta:
queryset = Task.objects.all()
authorization = Authorization()
allowed_methods = ['post']
resource_name = "create_task"
当我使用 Postman 指定 parent_task_id 时,我无法创建任务。
{
"title": "ABCDERT",
"description": "world this week",
"due_date": "2018-11-12 1:2:1",
"parent_task_id_id": "2"
}
这是我在执行此操作时收到的错误消息:
"error_message": "An incorrect URL was provided '2' for the 'CreateTaskResource' resource.",
您应该指定 parent_task 的 uri
而不是 id
,例如
{
"title": "ABCDERT",
"description": "world this week",
"due_date": "2018-11-12 1:2:1",
"parent_task_id_id": "/create_task/2"
}
另外,这样定义资源中的外键字段是不正确的,
class TaskResource(ModelResource):
parent_task_id_id = fields.ToOneField('self', 'id', null=True, full=True)
详情可以看docs
我调整你的例子,比如: 型号:
class Task(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
parent_task = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True)
api.py
class TaskResource(ModelResource):
parent_task = fields.ToOneField('self', 'parent_task', null=True,
full=True)
class Meta:
queryset = Task.objects.all()
authorization = Authorization()
allowed_methods = ['post', 'get']
filtering = {'id': ALL, 'parent_task': ALL_WITH_RELATIONS}
resource_name = "task"
- 创建任务
POST身材喜欢:
{
"title": "task2",
"description": "world this week",
"due_date": "2018-11-12 1:2:1",
"parent_task":"/api/v1/task/1/"
}
- 外键查询
GET 参数如:
0.0.0.0:8000/api/v1/task/?parent_task__id=1