I'm trying to achieve the following using graphene-django's DjangoFilterConnectionField
:
{
allUsers(username_Icontains: "abc") {
edges {
node {
id
demographics (name_Icontains: "xyz") {
id
name
}
}
}
}
}
I know that in graphene django, it's possible to have nested filtering using graphene's List But I'm not sure if I can work this out using DjangoFilterConnectionField
.
I have the following graphene (relay) schema:
class UserNode(DjangoObjectType):
class Meta:
model = User
interfaces = (Node,)
filter_fields = {
'username': ['exact', 'icontains', 'in'],
}
class DemographicNode(DjangoObjectType):
class Meta:
model = Demographic
interfaces = (Node,)
filter_fields = {
'name': ['icontains'],
}
class Query(ObjectType):
user = Node.Field(UserNode)
all_users = DjangoFilterConnectionField(UserNode)
demographic = Node.Field(DemographicNode)
all_demographics = DjangoFilterConnectionField(DemographicNode)
In the docs it's suggested to introduce each filter on the connected node as well. So it would be like this:
class UserNode(DjangoObjectType):
class Meta:
model = User
interfaces = (Node,)
filter_fields = {
'username': ['exact', 'icontains', 'in'],
'demographic__name': ['icontains']
}
But I think there must be a better way to do this since I have to do this for more than 20 nested Nodes.
I think you have OneToOneField between this two model, if it's true, you can't have something like that because it's not logical
imagine that, if a user has a Demographic, and you retrieve allUsers, then for each one, we have one Demographic and it's not logical to filter one Demographic. the only way to do this is to filter it with the UserNode(as you said and did)
the Nested filtering just work if you have a ForeignKey or ManyToManyField to user model and then you can retrieve that in user with the separate filter
I hope I've been able to make it clear to you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.