

def fields_for_document(document, fields=None, exclude=None, formfield_callback=None):
    '\n    Returns a ``SortedDict`` containing form fields for the given model.\n\n    ``fields`` is an optional list of field names. If provided, only the named\n    fields will be included in the returned fields.\n\n    ``exclude`` is an optional list of field names. If provided, the named\n    fields will be excluded from the returned fields, even if they are listed\n    in the ``fields`` argument.\n    '
    field_list = []
    structure = document.structure
    for (field_name, field_type) in structure.items():
        if (fields and (not (field_name in fields))):
            continue
        if (exclude and (field_name in exclude)):
            continue
        form_field = None
        if formfield_callback:
            form_field = formfield_callback(document, field_name)
        if (not form_field):
            form_field = formfield_for_document_field(document, field_name)
        if form_field:
            field_list.append((field_name, form_field))
    field_dict = SortedDict(field_list)
    if fields:
        field_dict = SortedDict([(f, field_dict.get(f)) for f in fields if ((not exclude) or (exclude and (f not in exclude)))])
    return field_dict

dataset/ETHPy150Open peterbe/django-mongokit/django_mongokit/forms/forms.py fields_for_document/original