def model_to_dict(model, recurse=True, backrefs=False, only=None,<EOL>exclude=None, seen=None, extra_attrs=None,<EOL>fields_from_query=None, max_depth=None, manytomany=False):
max_depth = -<NUM_LIT:1> if max_depth is None else max_depth<EOL>if max_depth == <NUM_LIT:0>:<EOL><INDENT>recurse = False<EOL><DEDENT>only = _clone_set(only)<EOL>extra_attrs = _clone_set(extra_attrs)<EOL>should_skip = lambda n: (n in exclude) or (only and (n not in only))<EOL>if fields_from_query is not None:<EOL><INDENT>for item in fields_from_query._returning:<EOL><INDENT>if isinstance(item, Field):<EOL><INDENT>only.add(item)<EOL><DEDENT>elif isinstance(item, Alias):<EOL><INDENT>extra_attrs.add(item._alias)<EOL><DEDENT><DEDENT><DEDENT>data = {}<EOL>exclude = _clone_set(exclude)<EOL>seen = _clone_set(seen)<EOL>exclude |= seen<EOL>model_class = type(model)<EOL>if manytomany:<EOL><INDENT>for name, m2m in model._meta.manytomany.items():<EOL><INDENT>if should_skip(name):<EOL><INDENT>continue<EOL><DEDENT>exclude.update((m2m, m2m.rel_model._meta.manytomany[m2m.backref]))<EOL>for fkf in m2m.through_model._meta.refs:<EOL><INDENT>exclude.add(fkf)<EOL><DEDENT>accum = []<EOL>for rel_obj in getattr(model, name):<EOL><INDENT>accum.append(model_to_dict(<EOL>rel_obj,<EOL>recurse=recurse,<EOL>backrefs=backrefs,<EOL>only=only,<EOL>exclude=exclude,<EOL>max_depth=max_depth - <NUM_LIT:1>))<EOL><DEDENT>data[name] = accum<EOL><DEDENT><DEDENT>for field in model._meta.sorted_fields:<EOL><INDENT>if should_skip(field):<EOL><INDENT>continue<EOL><DEDENT>field_data = model.__data__.get(field.name)<EOL>if isinstance(field, ForeignKeyField) and recurse:<EOL><INDENT>if field_data:<EOL><INDENT>seen.add(field)<EOL>rel_obj = getattr(model, field.name)<EOL>field_data = model_to_dict(<EOL>rel_obj,<EOL>recurse=recurse,<EOL>backrefs=backrefs,<EOL>only=only,<EOL>exclude=exclude,<EOL>seen=seen,<EOL>max_depth=max_depth - <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>field_data = None<EOL><DEDENT><DEDENT>data[field.name] = field_data<EOL><DEDENT>if extra_attrs:<EOL><INDENT>for attr_name in extra_attrs:<EOL><INDENT>attr = getattr(model, attr_name)<EOL>if callable_(attr):<EOL><INDENT>data[attr_name] = attr()<EOL><DEDENT>else:<EOL><INDENT>data[attr_name] = attr<EOL><DEDENT><DEDENT><DEDENT>if backrefs and recurse:<EOL><INDENT>for foreign_key, rel_model in model._meta.backrefs.items():<EOL><INDENT>if foreign_key.backref == '<STR_LIT:+>': continue<EOL>descriptor = getattr(model_class, foreign_key.backref)<EOL>if descriptor in exclude or foreign_key in exclude:<EOL><INDENT>continue<EOL><DEDENT>if only and (descriptor not in only) and (foreign_key not in only):<EOL><INDENT>continue<EOL><DEDENT>accum = []<EOL>exclude.add(foreign_key)<EOL>related_query = getattr(model, foreign_key.backref)<EOL>for rel_obj in related_query:<EOL><INDENT>accum.append(model_to_dict(<EOL>rel_obj,<EOL>recurse=recurse,<EOL>backrefs=backrefs,<EOL>only=only,<EOL>exclude=exclude,<EOL>max_depth=max_depth - <NUM_LIT:1>))<EOL><DEDENT>data[foreign_key.backref] = accum<EOL><DEDENT><DEDENT>return data<EOL>
Convert a model instance (and any related objects) to a dictionary.

:param bool recurse: Whether foreign-keys should be recursed.
:param bool backrefs: Whether lists of related objects should be recursed.
:param only: A list (or set) of field instances indicating which fields
    should be included.
:param exclude: A list (or set) of field instances that should be
    excluded from the dictionary.
:param list extra_attrs: Names of model instance attributes or methods
    that should be included.
:param SelectQuery fields_from_query: Query that was source of model. Take
    fields explicitly selected by the query and serialize them.
:param int max_depth: Maximum depth to recurse, value <= 0 means no max.
:param bool manytomany: Process many-to-many fields.