Fork me on GitHub

1 Database and collection

Suppose we have database db1, collelction user_collection1.

1.1 Data format

For each record, we have following fields:

1.2 Import sample data to MongoDB

Download sample data here: data_100.json

mongoimport -d db1 -c user_collection1 --file data_100.json

2, Add application to your Django project

add djmongoreader to INSTALLED_APPS in settings.py

INSTALLED_APPS = (
    ...
    'djmongoreader',
)

3, Customize dj-mongo-reader

3.1 Django setting

in setting.py

        MONGO_READER_SETTINGS = {
            'conn_str': os.getenv('MONGOLAB_URI', 'mongodb://127.0.0.1:27017/db1'),
            'perm_check_func': 'sampleapp.security.my_mongocall_perm_check'
        }
    

two keys in dj-mongo-reader setting dict:

The cmd paramter in perm check function may be find, status, count, exportcsv

3.2 Add result HTML slice to your application

In application's page query.html(source code), add:

{% include 'dj-mongo-reader/table.html' %}

3.3 Customize the result page

Create templates/dj-mongo-reader/table.html( source code), modify the HTML elements in Django blockdj-mongo-rest-table remember to add extends statement.

{% extends "dj-mongo-reader/core.html"%}

3.4 Display options in backend

Code in views.py(source code)to render the page contains the form and result table.

from djmongoreader.models import mongoReader
def query(request):
    context = RequestContext(request)
    _dbname = mongoReader.get_dbname_in_uri()
    dbname = _dbname if _dbname else "db1"
    args = {"db": dbname,
            "col": "user_collection1",
            "rowcount": 10,
            "sort": json.dumps({"name": -1}),
            "columns": "name,disable,gender,lastlogin",
            "columns_trans": json.dumps({"name": "user name", "lastlogin": "last login"})
    }
    return render_to_response("query.html", args, context_instance=context)
    

These parameters provide an initial query and rendering options:

  • db: @string, database name in MongoDB. if you had provided database name in connection URI, you can fill the value here by mongoReader.get_dbname_in_uri().
  • col: @string, collection name in MongoDB.
  • rowcount: @int, number of result in one page. optional, default: 50.
  • sort: @json_str, sort statement .optional, default: no sort.
  • columns: @string, columns show in the result table. format: field names connected by comma.
  • columns_trans: @json_str, database field name to display name mapping. optional, default: use database field name directly.

All these parameters passed to frontend will be assigned to same-named variables in Javascript, and can be modified by demands.

3.5 Display options in frontend

Data transformation:

In query.html(source code) or its corresponding Javascript file, for each field you want to make a transformation, write a js function named in format <field name>_process. The function accept only one parameter, the raw value read from MongoDB.

1, Show Male if raw data in field gender is 0, otherwise show Female

window.gender_process = function (val) {
    return (val ? 'Female' : 'Male');
};
    

2, Show different icon for field disable field.

window.disable_process = function (val) {
    return (val ? '<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>' : '<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>');
};
    

4 Export records to CSV file

URL endpoint: /<db>/<collection>/exportcsv/
Supported HTTP GET parameters:
e.g.
/db1/dbcollection_1/exportcsv/?criteria={"gender":0}&sort={"name":-1}&projection={"name":"student name", "gender":"sex","lastlogin":"last login"}
response
sex,student name,last login
0,user 39,2011-11-21 10:57:15.166000
0,user 41,2011-09-22 10:57:15.166000
0,user 43,2011-07-24 10:57:15.166000
0,user 45,2011-05-25 10:57:15.166000
0,user 47,2011-03-26 10:57:15.166000
0,user 49,2011-01-25 10:57:15.166000
0,user 51,2010-11-26 10:57:15.166000
0,user 53,2010-09-27 10:57:15.166000
0,user 55,2010-07-29 10:57:15.166000
...