def model_fields(model, fields=None, readonly_fields=None, exclude=None,
                 field_args=None, converter=None):
    """
    Generate a dictionary of WTForms fields for a given MongoEngine model.

    See `model_form` docstring for description of parameters.
    """
    from mongoengine.base import BaseDocument
    if BaseDocument not in inspect.getmro(model):
        raise TypeError('Model must be a MongoEngine Document schema')

    readonly_fields = readonly_fields or []
    exclude = exclude or []

    converter = converter or ModelConverter()
    field_args = field_args or {}

    field_names = fields if fields else model._fields.keys()
    field_names = (x for x in field_names if x not in exclude)

    field_dict = {}
    for name in field_names:
        if name not in readonly_fields and name not in model._fields:
            raise KeyError('"%s" is not read-only and does not appear to be a field on the document.' % name)

        if name in model._fields and name not in readonly_fields:
            model_field = model._fields[name]
            field = converter.convert(model, model_field, field_args.get(name))
            if field is not None:
                field_dict[name] = field

    return field_dict
dataset/ETHPy150Open syrusakbary/Flask-SuperAdmin/flask_superadmin/model/backends/mongoengine/orm.py/model_fields/original