How can I delete an attribute when a flush or commit has finished in a MapperExtension?
I was trying to get the last value "before_insert" a new item but I didn't find out, so I changed my strategy and I saved the last value of this method, but the problem is that when I do other commits my old MapperExtension attribute (self.last_value) gets the last value and that isn't what I want. This happens even when I close and open a new Session (discovered in my tests).
This is my code:
class _ValueExtension(MapperExtension):
"""Set new value for SkuDynamic and SKuUnique"""
def __init__(self):
self.last_value = {} # <<--- THE PROBLEM
self.first_value = FIRST_VALUE
def before_insert(self, mapper, connection, instance):
session = object_session(instance)
cls = instance.__class__
last_sku_value = self.get_last_value(session, cls, instance)
if not last_sku_value:
instance.value = self.first_value
else:
instance.value = next_value(last_sku_value)
self.last_value[self.last_value_key] = instance.value
self.set_values(instance)
#def after_insert(self, mapper, connection, instance):
# self.__init__() #<--- IF I DO THIS I DON'T HAVE THE LAST VALUE IN THE SAME FLUSH
开发者_如何学JAVA #def after_update(self, mapper, connection, instance):
# self.__init__()
def get_last_value(self, session, cls, instance):
"""This have to return just one value
and set self.last_value_key is nedded to update elements in same session
Execute The Query or check self.last_value
"""
raise NotImplementedError, 'implement this function'
def set_values(self, instance):
"""Use if necesary"""
pass
精彩评论