def get_values(instance, go_into={}, exclude=(), extra=(), skip_none=False):
from django.db.models.manager import Manager<EOL>from django.db.models import Model<EOL>SIMPLE_TYPES = (int, long, str, list, dict, tuple, bool, float, bool,<EOL>unicode, NoneType)<EOL>if not isinstance(instance, Model):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>value = {<EOL>'<STR_LIT>': instance.pk,<EOL>}<EOL>if isinstance(go_into, str):<EOL><INDENT>go_into = {go_into: {}}<EOL><DEDENT>if isinstance(exclude, str):<EOL><INDENT>exclude = (exclude,)<EOL><DEDENT>if isinstance(extra, str):<EOL><INDENT>extra = (extra,)<EOL><DEDENT>for field in extra:<EOL><INDENT>property = getattr(instance, field)<EOL>if callable(property):<EOL><INDENT>property = property()<EOL><DEDENT>if skip_none and property is None:<EOL><INDENT>continue<EOL><DEDENT>elif isinstance(property, SIMPLE_TYPES):<EOL><INDENT>value[field] = property<EOL><DEDENT>else:<EOL><INDENT>value[field] = repr(property)<EOL><DEDENT><DEDENT>field_options = instance._meta.get_all_field_names()<EOL>for field in field_options:<EOL><INDENT>try:<EOL><INDENT>property = getattr(instance, field)<EOL><DEDENT>except:<EOL><INDENT>continue<EOL><DEDENT>if skip_none and property is None:<EOL><INDENT>continue<EOL><DEDENT>if field in exclude or field[<NUM_LIT:0>] == '<STR_LIT:_>' or isinstance(property, Manager):<EOL><INDENT>continue<EOL><DEDENT>elif go_into.has_key(field):<EOL><INDENT>try:<EOL><INDENT>field_go_into = go_into[field].get('<STR_LIT>', {})<EOL><DEDENT>except AttributeError:<EOL><INDENT>field_go_into = {}<EOL><DEDENT>try:<EOL><INDENT>field_exclude = go_into[field].get('<STR_LIT>', ())<EOL><DEDENT>except AttributeError:<EOL><INDENT>field_exclude = ()<EOL><DEDENT>try:<EOL><INDENT>field_extra = go_into[field].get('<STR_LIT>', ())<EOL><DEDENT>except AttributeError:<EOL><INDENT>field_extra = ()<EOL><DEDENT>value[field] = get_values(property,<EOL>field_go_into,<EOL>field_exclude,<EOL>field_extra, skip_none=skip_none)<EOL><DEDENT>else:<EOL><INDENT>if isinstance(property, Model):<EOL><INDENT>value[field] = property.pk<EOL><DEDENT>elif isinstance(property, (datetime.date,<EOL>datetime.time,<EOL>datetime.datetime)):<EOL><INDENT>value[field] = property<EOL><DEDENT>else:<EOL><INDENT>if callable(property):<EOL><INDENT>property = property()<EOL><DEDENT>if isinstance(property, SIMPLE_TYPES):<EOL><INDENT>value[field] = property<EOL><DEDENT>else:<EOL><INDENT>value[field] = repr(property)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return value<EOL>
Transforms a django model instance into an object that can be used for
serialization.
@param instance(django.db.models.Model) - the model in question
@param go_into(dict) - relations with other models that need expanding
@param exclude(tuple) - fields that will be ignored
@param extra(tuple) - additional functions/properties which are not fields
@param skip_none(bool) - skip None field

Usage:
get_values(MyModel.objects.get(pk=187),
           {'user': {'go_into': ('clan',),
                     'exclude': ('crest_blob',),
                     'extra': ('get_crest_path',)}},
           ('image'))