Skip to main content

import

Project description

mongoengine datatables

A script for using the jQuery plug-in DataTables server-side processing with MongoEngine.

Works with Flask and Django. Supports column sorting and filtering by multiple search terms. Supports ReferenceFields & EmbeddedDocumentField for search.

Downloads

Install

You can install with pip:

pip install mongoengine_datatables

Minimal example mongoengine datatables

Copy paste this code to app.py

from flask import Flask, render_template_string, jsonify, request
from flask_mongoengine import MongoEngine
from mongoengine_datatables import DataTables
app = Flask(__name__)
db = MongoEngine(app)


class Hello(db.Document):
    field_one = db.StringField()
    field_two = db.StringField()


@app.route('/api/', methods=['POST'])
def api():
    args = request.get_json()
    res = DataTables(Hello, args).get_rows()
    return jsonify(res)


@app.route('/')
def hello_world():
    if not Hello.objects(field_one='Hello').first():
        Hello(field_one='Hello', field_two='World').save()
    return render_template_string(
        '''
    <html> <head>
        <link rel="stylesheet" type="text/css"
        href="https://cdn.datatables.net/v/bs4-4.1.1/jq-3.3.1/jszip-2.5.0/dt-1.10.20/b-1.6.1/b-html5-1.6.1/r-2.2.3/sc-2.0.1/sl-1.3.1/datatables.min.css"/>
    </head>
    <body class="p-3">
          <table id="dt_table" class="table table-striped" style="width:100%">
            <thead>
            <tr>
                <th>one</th>
                <th>two</th>
            </tr>
            </thead>
        </table>
    </body>
    <script type="text/javascript"
        src="https://cdn.datatables.net/v/bs4-4.1.1/jq-3.3.1/jszip-2.5.0/dt-1.10.20/b-1.6.1/b-html5-1.6.1/r-2.2.3/sc-2.0.1/sl-1.3.1/datatables.min.js"></script>
    <script>
        $(function () {
            $('#dt_table').DataTable({
                serverSide: true,
                ajax: {
                    url: '{{ url_for('api') }}',
                    dataSrc: 'data',
                    type: 'POST',
                    contentType: 'application/json',
                    data: function (d) {
                        return  JSON.stringify(d)
                    }
                },
                columns: [
                    {data: 'field_one'},
                    {data: 'field_two'},
                ]
            });
        });
    </script> </html>
        '''
    )


if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0')

Install modules & run:

python app.py

Results:

Advanced usage:

embed_search - For specific search in EmbeddedDocumentField q_obj - Additional search results in reference collection custom_filter - Your custom filter

DataTables(your_model, request_args, embed_search={}, q_obj=[], custom_filter={})

For datetime field

DataTables return datetime as epoch time with milliseconds. Example:

{'my_date': {'$date': 1605116909954}}

You can convert it:

import time
time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(1605116909954/1000.0))

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

mongoengine_datatables-0.1.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

mongoengine_datatables-0.1.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file mongoengine_datatables-0.1.2.tar.gz.

File metadata

  • Download URL: mongoengine_datatables-0.1.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.23.0

File hashes

Hashes for mongoengine_datatables-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a3a57a7e6a9589bdd020c48839cffeca7db94d7790fbef6802c9f80057861056
MD5 4c449ee5f759149499cdf9b6db1c6bee
BLAKE2b-256 0578e67e9a5877773ec798b65095c9583b0667366024b48e5123aa4ba1d8f970

See more details on using hashes here.

File details

Details for the file mongoengine_datatables-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mongoengine_datatables-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ebb6cf8d9ae6b745aadbf21fca1459402c18738830f06520ddef665de7119723
MD5 4f2bcf3dcba2e222e0ec0b0c7fdf6160
BLAKE2b-256 84bbd03b52bfc2eb57f9969d680bf911b6ee19a44c3c6c71c1619b04689bd35b

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