I have 2 models:
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)
And 2 Schemas:
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)
I can query the city_count
on countries
, but I can't seem to filter on it (exact, or gte greater than / lte less than)
i.e. this works:
{
countries(first: 10) {
edges {
node {
name
cityCount
}
}
}
}
but this doesn't:
{
countries(cityCount: 5) {
edges {
node {
name
cityCount
}
}
}
}
and triggers the following error:
TypeError at /api/graphql/
'Meta.fields' must not contain non-model field names: city_count
Any idea how I can filter/order on non-model fields?