Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

[UNMAINTAINED] Filtering tools for django rest framework mongoengine.

License

Notifications You must be signed in to change notification settings

qwiglydee/drf-mongo-filters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

drf-mongo-filters

Filtering tools for django rest framework mongoengine. Very similar to django-filters.

Sinopsis

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

Implemented filters

  • BooleanFilter: parses boolean val using NullBooleanField
  • ExistsFilter: parses boolean, filters with foo_exists=val
  • CharFilter: takes string
  • UUIDFilter: parses uuid
  • IntegerFilter: parses int
  • FloatFilter: parses float
  • DateTimeFilter: parses datetime using serializers.DateTimeField
  • DateFilter: parses date, filters datetimes with with gte and lte to match whole day
  • ObjectIdFilter: parses bson.ObjectId
  • ListFilter: gathers all values with same name; optionally parses with field, specified with argument child
  • AnyFilter: filters with foo_in=[vals]
  • NoneFilter: filters with foo_nin=[vals]
  • AllFilter: filters with foo_all=[vals]
  • DictFilter: gathers all values prefixed with same name; optionally parses with field, specified with argument child
  • RangeFilter: takes foo.min&foo.max and flters with gte and lte
  • GeoNearFilter: parses geopoint from foo.lng&foo.lat, converts to GeoJSON Point and filters with near operator
  • GeoDistanceFilter: parses float and filters with max_distance operator

API

See docstrings for details.

Filter

class

Attributes:

  • field_class: class used to create serializer field
  • lookup_type: operator to use for matching
Filter(lookup=None, name=None, **kwargs)

Args:

  • lookup: override matching operator
  • name: override binding name
  • kwargs: args to initialize field, in particular: source specifies model attribute to compare

Filterset

Filterset(data=None)

Arg:

  • data: QueryDict or dict containing filtering params
filter_queryset(queryset)

Applies all filters to queryset.

ModelFilterset

class

Attribs:

  • default_filters_mapping: mapping of field classes to filter classes
  • filters_mapping: to override defaults

Meta:

  • model: document definition to examine
  • fields: restrict fields to given list
  • exclude: exclude fields from examining
  • kwargs: mapping of field names to args for filters

About

[UNMAINTAINED] Filtering tools for django rest framework mongoengine.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages