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