我试图获取最后一个值“before_insert”一个新项目,但我没有找到,所以我改变了我的策略,我保存了这个方法的最后一个值,但问题是当我做其他提交时我的旧MapperExtension属性(self.last_value)获取最后一个值,这不是我想要的。即使我关闭并打开一个新的会话(在我的测试中发现),也会发生这种情况。
这是我的代码:
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
#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