GAE/P transaction using static method
Question: I'm trying to figure out how to organize app engine code with transactions.
Currently I have a separate python file with all my transaction functions. For
transactions that are closely related to entities, I was wondering if it made
sense to use a `@staticmethod` for the transaction.

Here is a simple example:

    
    
    class MyEntity(ndb.Model):
        n = ndb.IntegerProperty(default=0)
    
        @staticmethod
        @ndb.transactional # does the order of the decorators matter?
        def increment_n(my_entity_key):
            my_entity = my_entity_key.get()
            my_entity.n += 1
            my_entity.put()
    
        def do_something(self):
            MyEntity.increment_n(self.key)
    

It would be nice to have `increment_n` associated with the entity definition,
but I have never seen anyone do this so I was wondering if this would be a bad
idea.

MY SOLUTION:

Following Brent's answer, I've implemented this:

    
    
    class MyEntity(ndb.Model):
        n = ndb.IntegerProperty(default=0)
    
        @staticmethod
        @ndb.transactional
        def increment_n_transaction(my_entity_key):
            my_entity = my_entity_key.get()
            my_entity.increment_n()
    
        def increment_n(self):
            self.n += 1
            self.put()
    

This way I can keep entity related code all in one place and I can easily use
the transactional version or not as needed.


Answer: Yes, it makes sense to use a `@staticmethod` in this case, since the function
doesn't use a class or an instance (`self`).

And yes, the order of decorators is important, as noted in @Kekito's later
answer.

