端点问题 - 无法发布post-foreignkey选项(DjangoREST)

时间:2016-12-13 06:31:45

标签: python django api django-models django-rest-framework

所以这就是我的用户api的样子:

{
"count": 1,
"next": null,
"previous": null,
"results": [
    {
        "id": 1,
        "username": "FOOBAR",
        "Todo_List": [
            {
                "url": "http://localhost:8000/todolist_api/1/",
                "default": true,
                "name": "Chores",
                "todos": [
                    {
                        "url": "http://localhost:8000/todos_api/1/",
                        "todolist_id": 1,
                        "name": "Laundry",
                    },
                    {
                        "url": "http://localhost:8000/todos_api/2/",
                        "todolist_id": 1,
                        "name": "Sweep floor",
                    }
                ]
            },
            {
                "url": "http://localhost:8000/todolist_api/2/",
                "default": true,
                "name": "Work",
                "todos": [
                    {
                        "url": "http://localhost:8000/todos_api/3/",
                        "todolist_id": 2,
                        "name": "Talk to Boss",
                    },
                    {
                        "url": "http://localhost:8000/todos_api/4/",
                        "todolist_id": 2,
                        "name": "Say Hello to Janitor",
                    }
                ]
            }
        ]
]
}

我有3个API端点:user_api,todolist_api和todos_api

我的todos_api出了问题。我设置了这样,当用户想要为特定的todos_list添加另一个待办事项时,他们必须向todos_api发送一个帖子请求,其中包含todolist的pk和todo的名称。

即:如果我想为" Chores"创建另一个待办事项,我会通过todolist_id = 1向todos_api发送一个帖子请求,并且名称="将计算机除尘&#34 34 ;. 是todolist_id的选项缺失的问题。无论如何,当我点击帖子时,我收到错误,说todolist_id不能为空。 我认为可能是因为这是一个外键关系。

这是我的serializer.py

class TodoSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Todo
        fields = ('url', 'todolist_id', 'name')

class TodoListSerializer(serializers.HyperlinkedModelSerializer):
    todos = TodoSerializer(many=True, read_only=True)

    class Meta:
        model = TodoList
        fields = ('url', 'default', 'name', 'todos')

class UserSerializer(serializers.HyperlinkedModelSerializer):
    Todo_List = TodoListSerializer(many=True, read_only=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'Todo_List')

这是我的models.py

class TodoList(models.Model):
    default = models.BooleanField(default=False)
    owner = models.ForeignKey('auth.User', related_name='todolist')
    name = models.CharField(max_length=100, blank=True, default='')
    def __str__(self):
        return str(self.name+'-'+unicode(self.owner))

class Todos(models.Model):
    todolist = models.ForeignKey(TodoList, related_name='todolist_id', on_delete=models.CASCADE)
    owner = models.ForeignKey('auth.User', related_name='foobar')
    name = models.CharField(max_length=100, blank=True, default='')
    def __str__(self):
        return str(self.name)

如果由于我与todo和todolist交换我的实际api信息而隐藏任何语法错误,以隐藏我的api的想法[问题与语法无关]。

1 个答案:

答案 0 :(得分:0)

实际上,您不应该需要todolist_id,因为您可以从父数据中获取它。

如果你真的离不开,你应该修改序列化程序的创建/更新代码,将父母的todolist作为参数传递给todo创建:

Todos.objects.create(todolist=todolist, ...)