Skip to main content

django-mongoengine-filter is a reusable Django application for allowing users to filter mongoengine querysets dynamically. It’s very similar to popular django-filter library and is designed to be used as a drop-in replacement (as much as it’s possible) strictly tied to MongoEngine.

Full documentation on Read the docs.

PyPI Version Supported Python versions Build Status Documentation Status GPL-2.0-only OR LGPL-2.1-or-later Coverage

Requirements

  • Python 3.7, 3.8, 3.9, 3.10 or 3.11.

  • MongoDB 3.x, 4.x, 5.x.

  • Django 2.2, 3.0, 3.1, 3.2, 4.0 or 4.1.

Installation

Install using pip:

pip install django-mongoengine-filter

Or latest development version:

pip install https://github.com/barseghyanartur/django-mongoengine-filter/archive/master.zip

Usage

Sample document

from mongoengine import fields, document
from .constants import PROFILE_TYPES, PROFILE_TYPE_FREE, GENDERS, GENDER_MALE

class Person(document.Document):

    name = fields.StringField(
        required=True,
        max_length=255,
        default="Robot",
        verbose_name="Name"
    )
    age = fields.IntField(required=True, verbose_name="Age")
    num_fingers = fields.IntField(
        required=False,
        verbose_name="Number of fingers"
    )
    profile_type = fields.StringField(
        required=False,
        blank=False,
        null=False,
        choices=PROFILE_TYPES,
        default=PROFILE_TYPE_FREE,
    )
    gender = fields.StringField(
        required=False,
        blank=False,
        null=False,
        choices=GENDERS,
        default=GENDER_MALE
    )

    def __str__(self):
        return self.name

Sample filter

import django_mongoengine_filter

class PersonFilter(django_mongoengine_filter.FilterSet):

    profile_type = django_mongoengine_filter.StringFilter()
    ten_fingers = django_mongoengine_filter.MethodFilter(
        action="ten_fingers_filter"
    )

    class Meta:
        model = Person
        fields = ["profile_type", "ten_fingers"]

    def ten_fingers_filter(self, queryset, name, value):
        if value == 'yes':
            return queryset.filter(num_fingers=10)
        return queryset

Sample view

With function-based views:

def person_list(request):
    filter = PersonFilter(request.GET, queryset=Person.objects)
    return render(request, "dfm_app/person_list.html", {"object_list": filter.qs})

Or class-based views:

from django_mongoengine_filter.views import FilterView

class PersonListView(FilterView):

    filterset_class = PersonFilter
    template_name = "dfm_app/person_list.html"

Sample template

<ul>
{% for obj in object_list %}
    <li>{{ obj.name }} - {{ obj.age }}</li>
{% endfor %}
</ul>

Sample requests

  • GET /persons/

  • GET /persons/?profile_type=free&gender=male

  • GET /persons/?profile_type=free&gender=female

  • GET /persons/?profile_type=member&gender=female

  • GET /persons/?ten_fingers=yes

Development

Testing

To run tests in your working environment type:

pytest -vrx

To test with all supported Python versions type:

tox

Running MongoDB

The easiest way is to run it via Docker:

docker pull mongo:latest
docker run -p 27017:27017 mongo:latest

Writing documentation

Keep the following hierarchy.

=====
title
=====

header
======

sub-header
----------

sub-sub-header
~~~~~~~~~~~~~~

sub-sub-sub-header
^^^^^^^^^^^^^^^^^^

sub-sub-sub-sub-header
++++++++++++++++++++++

sub-sub-sub-sub-sub-header
**************************

License

GPL-2.0-only OR LGPL-2.1-or-later

Support

For any security issues contact me at the e-mail given in the Author section.

For overall issues, go to GitHub.

Author

Artur Barseghyan <artur.barseghyan@gmail.com>

Release files for django-mongoengine-filter 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-mongoengine-filter 0.4.2
File Size Uploaded
django-mongoengine-filter-0.4.2.tar.gz 37.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-mongoengine-filter 0.4.2
File Interpreter ABI Platform
django_mongoengine_filter-0.4.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 56.6 kB

Release files / django-mongoengine-filter-0.4.2.tar.gz

Download URL django-mongoengine-filter-0.4.2.tar.gz
Size 37.1 kB
Tags Source
SHA-256 checksum
How to use checksums
597b9807e8210c0eb70419ac8d28993d30b3bc7c56cd796bebd4a6cc09ac33e4
BLAKE2b-256 checksum
How to use checksums
78e08192ef56230b876751d37a22a5944822368ecd2abe6dc3520d34d9b159a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.16

Release files / django_mongoengine_filter-0.4.2-py2.py3-none-any.whl

Download URL django_mongoengine_filter-0.4.2-py2.py3-none-any.whl
Size 19.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
344f65c09b09475447a7903445983426b7146cb55d528be733c48cf32307b6d7
BLAKE2b-256 checksum
How to use checksums
06751082a3bb941398035c3be5993258dbc6ee335f17b0beb1be4c4dc256f1d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.16

Release history Release notifications | RSS feed

This release

0.4.2 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page