code: def model_fields(model, db_session=None, only=None, exclude=None,
                   field_args=None, converter=None, exclude_pk=False,
                   exclude_fk=False):
      mapper = model._sa_class_manager.mapper
      converter = converter or _ModelConverter()
      field_args = field_args or {}
      properties = []
      for prop in mapper.iterate_properties:
          if getattr(prop, 'columns', None):
              if exclude_fk and prop.columns[0].foreign_keys:
                  continue
              elif exclude_pk and prop.columns[0].primary_key:
                  continue
          properties.append((prop.key, prop))
      if only is not None:
          properties = (x for x in properties if x[0] in only)
      elif exclude:
          properties = (x for x in properties if x[0] not in exclude)
      field_dict = {}
      for name, prop in properties:
          field = converter.convert(
              model, mapper, prop,
              field_args.get(name), db_session
          )
          if field is not None:
              field_dict[name] = field
      return field_dict