Skip to content

Using exact filter on Relay with ForeignKey model #808

Closed
@sunnysid3up

Description

@sunnysid3up

I found a problem when I try to filter a model with ForeignKey. I expect to get the result below if the ID is incorrect.

{
    "data": {
        "forms": {
            "edges": []
        }
    }
}

However, I get every single row in my table returned if the value of ID is incorrect. Filtering works as expected if the ID exists.

# models
class Province(models.Model):
    name = models.CharField(max_length=50)

class City(models.Model):
    province = models.ForeignKey(Province,
                                 on_delete=models.CASCADE,
                                 related_name='city')
    name = models.CharField(max_length=50)
# schema
class ProvinceNode(DjangoObjectType):
    class Meta:
        model = Province
        filter_fields = {'name': ['exact']}
        interfaces = (relay.Node, )


class CityNode(DjangoObjectType):
    class Meta:
        model = City
        filter_fields = {'province': ['exact']}
        interfaces = (relay.Node, )

cities = DjangoFilterConnectionField(queries.CityNode)
# query
{
  cities(province: "UHJvdmluY2VOb2RlOjE3") {
    edges {
      node { id name province { id name } }
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions