From: Mike Bayer Date: Sun, 23 Apr 2006 20:37:53 +0000 (+0000) Subject: added temporary option "construct_new" to mapper which will cause the mapper to use... X-Git-Tag: rel_0_1_7~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a8fe116387e075395b61f1ae2a09551d174467a0;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git added temporary option "construct_new" to mapper which will cause the mapper to use __new__ to create loaded instances instead of the __init__ method --- diff --git a/lib/sqlalchemy/mapping/mapper.py b/lib/sqlalchemy/mapping/mapper.py index 302cb17411..4a26a4f38a 100644 --- a/lib/sqlalchemy/mapping/mapper.py +++ b/lib/sqlalchemy/mapping/mapper.py @@ -49,6 +49,7 @@ class Mapper(object): entity_name = None, always_refresh = False, version_id_col = None, + construct_new = False, **kwargs): if primarytable is not None: @@ -73,6 +74,7 @@ class Mapper(object): self._options = {} self.always_refresh = always_refresh self.version_id_col = version_id_col + self.construct_new = construct_new if not issubclass(class_, object): raise ArgumentError("Class '%s' is not a new-style class" % class_.__name__) @@ -724,7 +726,7 @@ class Mapper(object): # plugin point instance = self.extension.create_instance(self, row, imap, self.class_) if instance is EXT_PASS: - instance = self.class_(_mapper_nohistory=True, _sa_entity_name=self.entity_name, _sa_session=session) + instance = self._create_instance(session) imap[identitykey] = instance isnew = True else: @@ -742,6 +744,21 @@ class Mapper(object): result.append_nohistory(instance) return instance + def _create_instance(self, session): + if not self.construct_new: + return self.class_(_mapper_nohistory=True, _sa_entity_name=self.entity_name, _sa_session=session) + + obj = self.class_.__new__(self.class_) + obj._entity_name = self.entity_name + + # this gets the AttributeManager to do some pre-initialization, + # in order to save on KeyErrors later on + objectstore.global_attributes.init_attr(obj) + + session._bind_to(obj) + + return obj + def translate_row(self, tomapper, row): """attempts to take a row and translate its values to a row that can be understood by another mapper. breaks the column references down to their