Filtering tools for django rest framework mongoengine. Very similar to django-filters.
Declare filters for each query argument and bind it to Filterset:
class SomeFilterset(Filterset):
foo = filters.CharFilter()
bar = filters.CharFilter('contains')
Apply filters to queryset:
fs = SomeFilterset(QueryDict("foo=Foo&bar=Bar"))
qs = fs.filter_queryset(qs)
# equal to
qs.filter(foo="Foo").filter(bar__contains="Bar")
Auto generate filters for each model field with equality comparision:
class SomeFilterset(ModelFilterset):
class Meta:
model=SomeDoc
Set backend to apply filtering automatically in GenericAPIView.filter_queryset
, usually called from ListModelMixin.list
:
class TestView(ListAPIView):
filter_backends = (MongoFilterBackend,)
filter_class = SomeFilterset
BooleanFilter
: parses boolean val usingNullBooleanField
ExistsFilter
: parses boolean, filters withfoo_exists=val
CharFilter
: takes stringUUIDFilter
: parses uuidIntegerFilter
: parses intFloatFilter
: parses floatDateTimeFilter
: parses datetime usingserializers.DateTimeField
DateFilter
: parses date, filters datetimes with withgte
andlte
to match whole dayObjectIdFilter
: parsesbson.ObjectId
ListFilter
: gathers all values with same name; optionally parses with field, specified with argumentchild
AnyFilter
: filters withfoo_in=[vals]
NoneFilter
: filters withfoo_nin=[vals]
AllFilter
: filters withfoo_all=[vals]
DictFilter
: gathers all values prefixed with same name; optionally parses with field, specified with argumentchild
RangeFilter
: takesfoo.min&foo.max
and flters withgte
andlte
GeoNearFilter
: parses geopoint fromfoo.lng&foo.lat
, converts to GeoJSON Point and filters withnear
operatorGeoDistanceFilter
: parses float and filters withmax_distance
operator
See docstrings for details.
Attributes:
field_class
: class used to create serializer fieldlookup_type
: operator to use for matching
Args:
lookup
: override matching operatorname
: override binding namekwargs
: args to initialize field, in particular:source
specifies model attribute to compare
Arg:
data
: QueryDict or dict containing filtering params
Applies all filters to queryset.
Attribs:
- default_filters_mapping: mapping of field classes to filter classes
- filters_mapping: to override defaults
Meta:
model
: document definition to examinefields
: restrict fields to given listexclude
: exclude fields from examiningkwargs
: mapping of field names to args for filters