jsonapi.mongoengine

This package contains the database adapter and schema for mongoengine documents. It will detect and add mongoengine fields to the jsonapi schema automatic.

Using mongoengine models with py-jsonapi is quite straightforward:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3

import mongoengine

import jsonapi
import jsonapi.mongoengine

class User(mongoengine.Document):

    name = mongoengine.StringField()
    email = mongoengine.EmailField()

class Post(mongoengine.Document):

    text = mongoengine.StringField()
    author = mongoengine.ReferenceField(User)

# Use the mongoengine schema for the models.
user_schema = jsonapi.mongoengine.Schema(User)
post_schema = jsonapi.mongoengine.Schema(Post)

# Create the mongoengine database adapter.
db = jsonapi.mongoengine.Database()

# Create the API
api = jsonapi.base.api.API("/api", db)
api.add_type(user_schema)
api.add_type(post_schema)

API

class jsonapi.mongoengine.schema.Schema(resource_class, typename=None)[source]

Bases: jsonapi.base.schema.Schema

This Schema subclass also finds mongoengine attributes and relationships.

Parameters:
  • resource_class – The mongoengine document (class)
  • typename (str) – The typename of the resources in the JSONapi. If not given, it is derived from the resource class.
find_mongoengine_markers()[source]

Finds all mongoengine attributes and relationships.

class jsonapi.mongoengine.database.Database(api=None)[source]

Bases: jsonapi.base.database.Database

This adapter must be chosen for mongoengine models. We assume that the database connection has been created with mongoengine.connect().

session()[source]