Skip to main content

django-mongoengine-filter is a reusable Django application inspired from django-filter for allowing mongoengine users to filter querysets dynamically.

Project description

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>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-mongoengine-filter-0.4.1.tar.gz (36.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_mongoengine_filter-0.4.1-py2.py3-none-any.whl (19.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-mongoengine-filter-0.4.1.tar.gz.

File metadata

File hashes

Hashes for django-mongoengine-filter-0.4.1.tar.gz
Algorithm Hash digest
SHA256 32f4f41aeb0c51d25e127e79fcfa1a01395ef03a30f493ae68fedd76655b3de2
MD5 a310ca3ee7f3149567a78b73310e0c8f
BLAKE2b-256 307ff5c6e4ff37476f4e9f7fc7cadb412b263751bc445837a3adb8a588fbea7e

See more details on using hashes here.

File details

Details for the file django_mongoengine_filter-0.4.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_mongoengine_filter-0.4.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c3d9d3095d82d1ff68b39466436e9e7aa3cd9efa85cde83cef2a1943dc38ff91
MD5 b50252852093720c107c7e671d884815
BLAKE2b-256 8f35638a8e54c7b9455fe9a059e94d4f310d0718912f89a0f0b7c6891088740b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page