0.7.6
=====
- orm
+ - [feature] Added "no_autoflush" context
+ manager to Session, used with with:
+ will temporarily disable autoflush.
+
- [bug] Fixed bug whereby MappedCollection
would not get the appropriate collection
instrumentation if it were only used
return self._query_cls(entities, self, **kwargs)
+ @property
+ @util.contextmanager
+ def no_autoflush(self):
+ """Return a context manager that disables autoflush.
+
+ e.g.::
+
+ with session.no_autoflush:
+
+ some_object = SomeClass()
+ session.add(some_object)
+ # won't autoflush
+ some_object.related_thing = session.query(SomeRelated).first()
+
+ Operations that proceed within the ``with:`` block
+ will not be subject to flushes occurring upon query
+ access. This is useful when initializing a series
+ of objects which involve existing database queries,
+ where the uncompleted object should not yet be flushed.
+
+ New in 0.7.6.
+
+ """
+ autoflush = self.autoflush
+ self.autoflush = False
+ yield self
+ self.autoflush = autoflush
+
def _autoflush(self):
if self.autoflush and not self._flushing:
self.flush()
from compat import callable, cmp, reduce, defaultdict, py25_dict, \
threading, py3k_warning, jython, pypy, win32, set_types, buffer, pickle, \
update_wrapper, partial, md5_hex, decode_slice, dottedgetter,\
- parse_qsl, any
+ parse_qsl, any, contextmanager
from _collections import NamedTuple, ImmutableContainer, immutabledict, \
Properties, OrderedProperties, ImmutableProperties, OrderedDict, \
buffer = buffer
# end Py2K
+try:
+ from contextlib import contextmanager
+except ImportError:
+ def contextmanager(fn):
+ return fn
+
try:
from functools import update_wrapper
except ImportError:
eq_(bind.connect().execute("select count(1) from users").scalar(), 1)
sess.close()
+ @testing.requires.python26
+ def test_with_no_autoflush(self):
+ User, users = self.classes.User, self.tables.users
+
+ mapper(User, users)
+ sess = Session()
+
+ u = User()
+ u.name = 'ed'
+ sess.add(u)
+ def go(obj):
+ assert u not in sess.query(User).all()
+ testing.run_as_contextmanager(sess.no_autoflush, go)
+ assert u in sess.new
+ assert u in sess.query(User).all()
+ assert u not in sess.new
+
def test_make_transient(self):
users, User = self.tables.users, self.classes.User