class GrapheneFilterSetMixin(BaseFilterSet): """ A django_filters.filterset.BaseFilterSet with default filter overrides to handle global IDs """ FILTER_DEFAULTS = dict( itertools.chain(FILTER_FOR_DBFIELD_DEFAULTS.items(), GRAPHENE_FILTER_SET_OVERRIDES.items()))
class GrapheneFilterSetMixin(BaseFilterSet): FILTER_DEFAULTS = dict( itertools.chain(FILTER_FOR_DBFIELD_DEFAULTS.items(), GRAPHENE_FILTER_SET_OVERRIDES.items())) @classmethod def filter_for_reverse_field(cls, f, name): """Handles retrieving filters for reverse relationships We override the default implementation so that we can handle Global IDs (the default implementation expects database primary keys) """ try: rel = f.field.remote_field except AttributeError: rel = f.field.rel default = {"name": name, "label": capfirst(rel.related_name)} if rel.multiple: # For to-many relationships return GlobalIDMultipleChoiceFilter(**default) else: # For to-one relationships return GlobalIDFilter(**default)
class BaseFilterSet(FilterSet): FILTER_DEFAULTS = dict(itertools.chain( FILTER_FOR_DBFIELD_DEFAULTS.items(), FILTER_OVERRIDES.items(), GRAPHENE_FILTER_SET_OVERRIDES.items(), )) id__in = GlobalIDInFilter(field_name='pk', distinct=True) pk__in = BaseInFilter(field_name='pk', distinct=True) sort_by = CharFilter(label='filter_sort_by', method='filter_sort_by') def filter_sort_by(self, queryset, name, value): return queryset.order_by(*value.strip().split(','))
class GraphQLFilterSetMixin(BaseFilterSet): FILTER_DEFAULTS = dict( itertools.chain(FILTER_FOR_DBFIELD_DEFAULTS.items(), GLOBAL_ID_FILTERS.items()))
class GrapheneFilterSetMixin(BaseFilterSet): FILTER_DEFAULTS = dict( itertools.chain(FILTER_FOR_DBFIELD_DEFAULTS.items(), GRAPHENE_FILTER_SET_OVERRIDES.items()))