Django 中继过滤器不适用于外键字段
Django relay filter not working for foreign key field
import graphene
from graphene_django import DjangoObjectType
from commodity.models import CommodityItem
class Brand(models.Model):
# more fields...
name = models.CharField(max_length=500)
class CommodityItem(models.Model):
# more fields...
is_online = models.BooleanField(default=True)
brand = models.ForeignKey(
Brand, on_delete=models.CASCADE, verbose_name='Brand', db_index=True)
class CommodityItemNode(DjangoObjectType):
class Meta:
model = CommodityItem
use_connection = True
description = 'Represents commodity item'
filter_fields = {
'is_online' : ['exact'],
'brand_id': ['exact']
}
interfaces = (graphene.relay.Node, )
all_commodity_item = DjangoFilterConnectionField(CommodityItemNode)
工作
query {
allCommodityItem(isOnline: true) {
edges {
node {
name
}
}
}
}
不工作
query {
allCommodityItem(isOnline: true, brandId: 1) {
edges {
node {
name
}
}
}
}
错误:
{
"status": "error",
"errors": [
{
"message": "['{\"brand_id\": [{\"message\": \"Invalid ID specified.\", \"code\": \"\"}]}']",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"allCommodityItem"
]
}
],
"data": {
"allCommodityItem": null
}
}
我在这里缺少什么?
参考: https://docs.graphene-python.org/projects/django/en/latest/tutorial-relay/
像这样添加 id 作为关系:
class CommodityItemNode(DjangoObjectType):
class Meta:
# rest of code
filter_fields = {
'is_online' : ['exact'],
'brand__id': ['exact']
}
在您的查询中:
query {
allCommodityItem(isOnline: true, brand_Id: "1") {
edges {
node {
name
}
}
}
}
import graphene
from graphene_django import DjangoObjectType
from commodity.models import CommodityItem
class Brand(models.Model):
# more fields...
name = models.CharField(max_length=500)
class CommodityItem(models.Model):
# more fields...
is_online = models.BooleanField(default=True)
brand = models.ForeignKey(
Brand, on_delete=models.CASCADE, verbose_name='Brand', db_index=True)
class CommodityItemNode(DjangoObjectType):
class Meta:
model = CommodityItem
use_connection = True
description = 'Represents commodity item'
filter_fields = {
'is_online' : ['exact'],
'brand_id': ['exact']
}
interfaces = (graphene.relay.Node, )
all_commodity_item = DjangoFilterConnectionField(CommodityItemNode)
工作
query {
allCommodityItem(isOnline: true) {
edges {
node {
name
}
}
}
}
不工作
query {
allCommodityItem(isOnline: true, brandId: 1) {
edges {
node {
name
}
}
}
}
错误:
{
"status": "error",
"errors": [
{
"message": "['{\"brand_id\": [{\"message\": \"Invalid ID specified.\", \"code\": \"\"}]}']",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"allCommodityItem"
]
}
],
"data": {
"allCommodityItem": null
}
}
我在这里缺少什么?
参考: https://docs.graphene-python.org/projects/django/en/latest/tutorial-relay/
像这样添加 id 作为关系:
class CommodityItemNode(DjangoObjectType):
class Meta:
# rest of code
filter_fields = {
'is_online' : ['exact'],
'brand__id': ['exact']
}
在您的查询中:
query {
allCommodityItem(isOnline: true, brand_Id: "1") {
edges {
node {
name
}
}
}
}