I am working on an application using Django. We've exposed the models using Graphene. We have the following models:
class Agent(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
name = models.CharField(
max_length=MAX_LENGTH,
null=True, blank=True,
help_text="Name of the agent"
)
teams = models.ManyToManyField(
'Team', blank=True,
help_text="The teams of which the user is part"
)
class Team(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
name = models.CharField(
max_length=MAX_LENGTH,
help_text="Name of the team"
)
Now I am trying to perform a filter on a ManyToMany field. But I can't seem to get it to work. I am trying to reproduce the following Django Query in GrapQL:
teams = [
Team.objects.get(id="6d6d1d4d-c1b4-485a-9269-f7e9d06d862a"),
Team.objects.get(id="7e082abf-584b-4858-b510-4c71fc295e2f"),
]
agents = Agent.objects.filter(teams__in=teams).distinct()
I tried to achieve this by enabling the 'in' filter on the node.
class AgentNode(DjangoObjectType):
class Meta:
model = Agent
filter_fields = {
"teams": ["in"]
}
Then I tried to execute the following query, but that didn't work as expected:
query getAllAgents{
allAgents(teams_In:["6d6d1d4d-c1b4-485a-9269-f7e9d06d862a, 7e082abf-584b-4858-b510-4c71fc295e2f"]) {
id
name
teams {
id
name
}
}
}
This gives me the following error:
{
"errors": [
{
"path": [
"allAgents"
],
"message": "'list' object has no attribute 'split'",
"locations": [
{
"column": 3,
"line": 2
}
]
}
],
"data": {
"allAgents": null
}
}
Does anyone know how I can get this to work? I've been breaking my head over it.
Python=2.7
Django==1.10.8
graphene-django==2.2.0
graphene-django-extras==0.3.5
django-filter==1.0.4
(I know the versions are old but for this project we have to work with them)