In fact i was only querying, but when i add many items in the same
flush, all items(of same category)get the same value, and i don't know
how to include the sku number already calculated to the query, that's
the reason because i tried this approach of saving "last_sku_value",
but i sow that something was really bad.

I tried to execute the query on __init__() of my class but i had to
pass the session to my object or take my session from a global so i
thought it was a bad idea.

And i could create my object add it to my session and after call a
function "class.new_sku()", but i dislike the idea to rewrite this for
each item.


this is my "get_last_value()" code, maybe it'll be clear:


   def get_last_value(self, session, cls, instance):
        """execute a query to find the last value"""
        try:
            return self.last_value[instance.dynamic.value]
        except KeyError:
            self.last_value_key = "%s-%s" %(instance.dynamic.static,
                                            instance.dynamic.value)

        last_sku_value = session.query(cls.value)\
                           .filter(cls.dynamic == instance.dynamic)\
                           .order_by(desc(cls.date_insert)).first()

        if not last_sku_value:
            return None
        try:
            return last_sku_value[0]
        except IndexError:
            return None



On 13 jul, 16:35, Michael Bayer <[email protected]> wrote:
> On Jul 13, 2010, at 10:21 AM, sacabuche wrote:
>
>
>
> > I was trying to get the last value "before_insert" a new item but i
> > didn't find out, so i change of 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) get the last value and i
> > dislike it, 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
>
> >    #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
>
> A MapperExtension is persistent for the life of the application and also is 
> accessed concurrently, so it is not an appropriate place to store a count for 
> things things that are local to a specific flush, and its really not a good 
> place in general to do what you're trying to do (though its not clear above 
> what you're trying to accomplish, if you want a global "last sku" or a "sku" 
> specific to one flush).
>
> You'd be better off querying the database for the "last sku value" when you 
> need it, or just adding code to your classes __init__() method to keep track 
> of sku numbers in memory as needed.
>
>
>
> > Thanks
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "sqlalchemy" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to 
> > [email protected].
> > For more options, visit this group 
> > athttp://groups.google.com/group/sqlalchemy?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to