This is what I wrote to create a dict of all fields and their nested types.
from mongoengine.fields import ( IntField, BooleanField, StringField, DateTimeField, DecimalField, FloatField, LongField, ListField, EmbeddedDocumentField, ReferenceField, ) __input_types = { IntField, BooleanField, StringField, DateTimeField, DecimalField, FloatField, LongField, } __input_num_types = { IntField, BooleanField, DecimalField, FloatField, LongField, } def list_schema(m): """list all the field in the model and in the nested models""" sdict = {} for k, v in m._fields.iteritems(): if k == '_cls': continue ftype = type(v) if ftype in __input_types: sdict[k] = { 'default': v.default if v.default else '', 'is_num': ftype in __input_num_types, 'required': v.required, } elif ftype == ListField: seqtype = v.field if seqtype in __input_types: sdict[k] = [{ 'default': v.default() if v.default else '', 'is_num': seqtype in __input_num_types, 'required': v.required, }] else: sdict[k] = [list_schema(seqtype.document_type)] else: sdict[k] = list_schema(v.document_type) return sdict
harry
source share