Mixin for connecting DataTables to MongoDB with MongoEngine.
Project description
MongoEngine DataTables
The DataTablesManager
class can be used instead of the default MongoEngine
QuerySet
class to add a datatables
method for returning results as required by the
jQuery plugin DataTables.
Installation
pip install git+git://github.com/pauljolsen/datatables-mongoengine.git@v0.1.4
Example
Here's an example for Flask.
models.py
from mongoengine import Document, StringField, ListField
from mongoengine_datatables import DataTablesManager
class Links(Document):
"""The MongoEngine ODM class for the links_links collection."""
meta = {
"collection": "links_links",
"queryset_class": DataTablesManager
}
name = StringField()
category = StringField()
link = StringField()
group = ListField()
routes.py
from flask import request, g, jsonify
from app import app
from app.models import Links
@app.route("/ajax/links", methods=["POST"])
def ajax_links():
"""Get results from MongoDB for DataTables."""
data = request.get_json()
custom_filter = {
'group': g.user.group
}
results = Links.objects.datatables(data, **custom_filter)
return jsonify(results)
Note that you can inject any filter you want server-side, like I do above to make sure the results all match the current user's group.
app.js
$(document).ready( function () {
$('#example').DataTable({
processing: true,
serverSide: true,
ajax: {
url: '/ajax/links',
dataSrc: 'data',
type: 'POST',
contentType: 'application/json',
data: function (d) {
return JSON.stringify(d)
}
},
columns: [
{ data: 'name'},
{ data: 'category'},
{ data: 'link'}
],
});
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
Built Distribution
Close
Hashes for datatables_mongoengine-0.1.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8270e5a5732f6d81aa127c0c0cf8d2b58f60f2763a3ea6d39b4037230fb4814 |
|
MD5 | 368b52cf6f332f47fde23a0a5eeab7ff |
|
BLAKE2b-256 | de5be1684ea003660247812738f607116e522619f3042f86c4a1e10293492e7d |
Close
Hashes for datatables_mongoengine-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f300586b5331fd42470ea6f1c4a72f453658e513c48ff8fcc62349af2b70f208 |
|
MD5 | f211c1c03846380ee331ee2e7f502e0b |
|
BLAKE2b-256 | 2c19d0fa85f68038b27ff6a9b222adb2e89b1d6d29707ce4e73543ca1b528fe1 |