Search code examples
djangographqlgraphene-pythongraphene-django

Filter not null dates with Django and GraphQL


We have a tour node with feauted_date (DateTime) field, that can also be nullable.

class TourFilter(OrderByMixin):
    class Meta:
        model = TourModel
        fields = {
            'featured_date': ['exact', 'lt', 'gt'],
        }

class TourNode(DjangoObjectType):
    class Meta:
        model = TourModel
        filter_fields = TourFilter.Meta.fields
        interfaces = (relay.Node, )

Currently we use featured_date for sorting with OrderMixin, but is there any way to filter them by not null?


Solution

  • You can use isnull-(doc) lookup as

    class TourFilter(OrderByMixin):
        class Meta:
            model = TourModel
            fields = {
                'featured_date': ['exact', 'lt', 'gt', 'isnull'],
            }