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 design to be used as a drop-in replacement (as much as it’s possible) strictly tied to MongoEngine.
Full documentation on Read the docs.
Requirements
Python 2.7, 3.5, 3.6, 3.7
Django 1.11, 2.0, 2.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
__all__ = ("Person",)
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
def person_list(request):
filter = PersonFilter(request.GET, queryset=Person.objects())
return render(request, "dfm_app/person_list.html", {"objects": filter.qs})
Sample template
<ul>
{% for obj in objects %}
<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:
./runtests.py
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/LGPL 2.1
Support
For any issues contact me at the e-mail given in the Author section.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-mongoengine-filter-0.3.tar.gz.
File metadata
- Download URL: django-mongoengine-filter-0.3.tar.gz
- Upload date:
- Size: 24.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
387d75f2c1e73ce71f3e771b689b163e1163ab7ff42580988617fb2285edee13
|
|
| MD5 |
17d08d003f9765ce704c5379f713924d
|
|
| BLAKE2b-256 |
4e04f05480b241e807d21ccd8d8e768f1d545bb67504e625f81f2c1f098c1557
|
File details
Details for the file django_mongoengine_filter-0.3-py2.py3-none-any.whl.
File metadata
- Download URL: django_mongoengine_filter-0.3-py2.py3-none-any.whl
- Upload date:
- Size: 32.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e4f65e37a8ecaa407a21a992bdc627b37b2cb18fedb7f69712e8b7b144cb775
|
|
| MD5 |
456f7e4388d3752074cd34f65216a854
|
|
| BLAKE2b-256 |
118f128cc4d04d48b5e6c18672c18ef142f887f5574430b540acf45e99ebdbf1
|