Django-Tastypie自己的孩子

时间:2016-12-02 18:46:17

标签: django tastypie

尝试为多个子任务制作api。 我有任务模型,可以将另一个任务作为父项:

class Task(models.Model):
    parent_task = models.ForeignKey("Task", null=True, blank=True)
    name = models.CharField(max_length=64)

    def __unicode__ (self):
        return self.name

现在我正试图制作tastypie资源:

class TaskResource(ModelResource):
    parent_task = fields.ForeignKey(TaskResource, 'parent_task', full=False) <-- ERROR HERE

    class Meta:
        queryset = Task.objects.all()
        resource_name = 'task'
        list_allowed_methods = ['get', 'put', 'post', 'delete']
        include_resource_uri = False

    def dehydrate(self, bundle, for_list=False):
        bundle.data["subtasks"] = "how?" <-- HOW??
        return bundle

感谢您的时间。

P.S。我需要这样的东西:

[
    {
        "id": 1,
        "name": "Task 1",
        "subtasks": [
            {
                "id": 1,
                "name": "Task 1",
                "subtasks": [...]
            }
        ]
    },
    {
        "id": 2,
        "name": "Task 2",
        "subtasks": "how?"
    }
]

1 个答案:

答案 0 :(得分:1)

几乎是Including child resources in a Django Tastypie API的副本,但不完全是。

  1. 所以你的第一个问题是你指定与自我错误的关系。它应该只是self

    parent_task = fields.ForeignKey('self', 'parent_task', null=True, full=False)
    
  2. 其次,请注意null=True - 父级可以是null

  3. 最后,您只需要添加另一个关系字段并询问完整的详细信息

    subtasks = fields.ToManyField('self', 'task_set', full=True)
    

    task_setrelated_name字段的Task.parent_task

  4. 结果代码是:

    class TaskResource(ModelResource):
        parent_task = fields.ForeignKey('self', 'parent_task', null=True, full=False)
        subtasks = fields.ToManyField('self', 'subtasks', full=True)
    
        class Meta:
            queryset = Task.objects.all()
            resource_name = 'task'
            list_allowed_methods = ['get', 'put', 'post', 'delete']
            include_resource_uri = False
    

    结果:

    {
        "meta": {
            "previous": null, 
            "total_count": 3,  
            "offset": 0,  
            "limit": 20, 
            "next": null
        },  
        "objects": [
            {
                "parent_task": null, 
                "subtasks": [
                    {
                        "parent_task": "/api/v1/task/1/", 
                        "subtasks": [], 
                        "id": 2,  
                        "name": "Root's Child 1"
                    },
                    {
                        "parent_task": "/api/v1/task/1/", 
                        "subtasks": [], 
                        "id": 3,  
                        "name": "Root's Child 2"
                    }
                ],
                "id": 1,  
                "name": "Root Task"
            },
            {
                "parent_task": "/api/v1/task/1/", 
                "subtasks": [], 
                "id": 2,  
                "name": "Root's Child 1"
            },
            {
                "parent_task": "/api/v1/task/1/", 
                "subtasks": [], 
                "id": 3,  
                "name": "Root's Child 2"
            }
        ]   
    }