问题描述
我有 2 个模型:
import uuid
from django.db import models
class Country(models.Model):
name = models.CharField(max_length=255,unique=True)
def city_count(self):
return self.city_set.count()
class City(models.Model):
country = models.ForeignKey('country.Country',on_delete=models.CASCADE)
name = models.CharField(max_length=255)
和 2 个架构:
import graphene
from graphene_django.filter import DjangoFilterConnectionField
from core.utils import ExtendedDjangoObjectType
from .models import Country as CountryModel,City as CityModel
class Country(ExtendedDjangoObjectType):
class Meta:
model = CountryModel
filter_fields = {
'name': ['exact'],# 'city_count': ['exact'] => this does not work!
}
interfaces = (graphene.relay.Node,)
city_count = graphene.Int()
def resolve_city_count(self,info):
return self.city_count()
class City(ExtendedDjangoObjectType):
class Meta:
model = CityModel
filter_fields = ['name']
interfaces = (graphene.relay.Node,)
class Query(graphene.ObjectType):
country = graphene.relay.Node.Field(Country)
countries = DjangoFilterConnectionField(Country)
city = graphene.relay.Node.Field(City)
cities = DjangoFilterConnectionField(City)
我可以查询 city_count
上的 countries
,但我似乎无法对其进行过滤(精确,或 gte 大于 / lte 小于)
即这有效:
{
countries(first: 10) {
edges {
node {
name
cityCount
}
}
}
}
但这不会:
{
countries(cityCount: 5) {
edges {
node {
name
cityCount
}
}
}
}
并触发以下错误:
TypeError at /api/graphql/
'Meta.fields' must not contain non-model field names: city_count
知道如何对非模型字段进行过滤/排序吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)