Flask Snippets

Snippets are unofficial and unmaintained.

This is an archived view of user-submitted snippets. Despite being hosted on the Flask site, they are not official. No Flask maintainer has curated or checked the snippets for security, correctness, or design.

Support PyMongo ObjectIDs in URLs

Posted by Armin Ronacher on 2013-01-27 @ 19:57 and filed in Database

If you are using MongoDB with Flask you might want to consider adding native support for ObjectIDs to the routing system. With that object IDs will be compressed in URLs and the routing system will reject invalid ObjectIDs automatically.

Converter Implementation

from flask import Flask
from werkzeug.routing import BaseConverter, ValidationError
from itsdangerous import base64_encode, base64_decode
from bson.objectid import ObjectId
from bson.errors import InvalidId

class ObjectIDConverter(BaseConverter):
    def to_python(self, value):
            return ObjectId(base64_decode(value))
        except (InvalidId, ValueError, TypeError):
            raise ValidationError()
    def to_url(self, value):
        return base64_encode(value.binary)

Example Usage

And here is a minimal example that shows how it works:

from flask import Flask

app = Flask(__name__)
app.url_map.converters['objectid'] = ObjectIDConverter

def show_user(user_id):
    return 'User ID: %r' % user_id

The object IDs are then automatically compressed by url_for and decompressed by the routing system. To test this you can navigate to http://localhost:5000/users/UQWFb53NMX6iFe0s in the above example which will then expand to ObjectId('5105856f9dcd317ea215ed2c')

This snippet by Armin Ronacher can be used freely for anything you like. Consider it public domain.