def fields_for_document(document, fields=None, exclude=None, widgets=None,
                        formfield_callback=None,
                        field_generator=_fieldgenerator):
    field_list = []
    if isinstance(field_generator, type):
        field_generator = field_generator()

    if formfield_callback and not isinstance(formfield_callback, Callable):
        raise TypeError('formfield_callback must be a function or callable')

    for name in document._fields_ordered:
        f = document._fields.get(name)
        if isinstance(f, ObjectIdField):
            continue
        if fields and f.name not in fields:
            continue
        if exclude and f.name in exclude:
            continue
        if widgets and f.name in widgets:
            kwargs = {'widget': widgets[f.name]}
        else:
            kwargs = {}

        if formfield_callback:
            formfield = formfield_callback(f, **kwargs)
        else:
            formfield = field_generator.generate(f, **kwargs)

        if formfield:
            field_list.append((f.name, formfield))

    field_dict = OrderedDict(field_list)
    if fields:
        field_dict = OrderedDict(
            [(f, field_dict.get(f)) for f in fields
                if ((not exclude) or (exclude and f not in exclude))]
        )

    return field_dict
Returns a ``SortedDict`` containing form fields for the given model.
dataset/ETHPy150Open jschrewe/django-mongodbforms/mongodbforms/documents.py/fields_for_document