如何解决如何在 Graphene Django 中过滤相关模型计数
我有 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 举报,一经查实,本站将立刻删除。