def serialize_object(self, obj, obj_pk=None):
        '''
        Serializes an object for it to be added to the index.

        :param obj: Object to be serialized. Optional if obj_pk is passed.
        :param obj_pk: Object primary key. Superseded by `obj` if available.
        :return: A dictionary representing the object as defined in the mapping.
        '''
        if not obj:
            try:
                # We're using `filter` followed by `values` in order to only fetch the required fields.
                obj = self.model.objects.filter(pk=obj_pk).values(*self.fields_to_fetch)[0]
            except Exception as e:
                raise ValueError('Could not find object of primary key = {} in model {} (model index class {}). (Original exception: {}.)'.format(obj_pk, self.model, self.__class__.__name__, e))

        serialized_object = {}

        for name, field in iteritems(self.fields):
            if hasattr(self, "prepare_%s" % name):
                value = getattr(self, "prepare_%s" % name)(obj)
            else:
                value = field.value(obj)

            serialized_object[name] = value

        return serialized_object
def serialize_object ( self , obj , obj_pk = None ) : if not obj : try : obj = self . model . objects . filter ( pk = obj_pk ) . values ( * self . fields_to_fetch ) [ 0 ] except Exception as e : raise ValueError ( 'Could not find object of primary key = {} in model {} (model index class {}). (Original exception: {}.)' . format ( obj_pk , self . model , self . __class__ . __name__ , e ) ) serialized_object = { } for name , field in iteritems ( self . fields ) : if hasattr ( self , "prepare_%s" % name ) : value = getattr ( self , "prepare_%s" % name ) ( obj ) else : value = field . value ( obj ) serialized_object [ name ] = value return serialized_object
Serializes an object for it to be added to the index.

        :param obj: Object to be serialized. Optional if obj_pk is passed.
        :param obj_pk: Object primary key. Superseded by `obj` if available.
        :return: A dictionary representing the object as defined in the mapping.