【发布时间】:2021-11-09 05:21:34
【问题描述】:
我使用 graphene-Django 构建一个 GraphQL API。我已成功创建此 API,但无法传递参数来过滤我的响应。我想使用模型中的锻造密钥进行过滤。
模型.py:
class TodoList(models.Model):
userId = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=150)
date = models.DateField(auto_now=True)
text = models.TextField()
schema.py:
class TodoView(DjangoObjectType):
class Meta:
model = TodoList
fields = ('id', 'title', 'date', 'text')
class UserType(DjangoObjectType):
class Meta:
model = User
class TodoQuery(graphene.ObjectType):
todoList = DjangoListField(TodoView)
def resolve_all_todo(root, info):
return TodoList.objects.filter(userId=2)
但是当我过滤它时,它会在结果中显示所有对象
当我发送这个请求时:
{
todoList {
id
title
date
text
}
}
我收到了这样的回复:
{
"data": {
"todoList": [
{
"id": "1",
"title": "test",
"date": "2021-09-13",
"text": "test todo"
},
{
"id": "2",
"title": "test2",
"date": "2021-09-13",
"text": "test2"
},
{
"id": "3",
"title": "admin2 test",
"date": "2021-09-13",
"text": "admin2 test"
},
{
"id": "4",
"title": "admin2 test2",
"date": "2021-09-14",
"text": "admin2 test2"
}
]
}
}
我错过了什么?还是我做错了什么?
【问题讨论】:
-
我相信解析器应该命名为“resolve_
”,在你的情况下为“resolve_ todoList”
标签: python-3.x django django-models graphene-python