So first off, in Python we do have the option of overriding a
builtin method called __setattr__(), which if you really wanted to
intercept most attribute set events, you could just do that. This
is heavy-handed though because it is called for all attribute sets
everywhere and it would spend a lot of time filtering out attributes
it doesn't care about.
Secondly, @validates and AttributeEvents are two versions of the
same thing - @validates is a shortcut for AttributeEvents, and the
AttributeEvents listener is set up on a per-attribute basis. So
there's not too much difference except how these are configured, and
using the AttributeEvents is better for programmatic attachment.
You can also set up such a listener for all mapped attributes
everywhere, but again this is too broad and such a listener again
wastes lots of time filtering on which events it cares about.
With that information out of the way, the general strategy to listen
to events on things that are mapped such that the things which we're
listening to is also dynamic / programmatic is to apply event
listeners to the attributes we care about. There's more events that
we make use of in order to set up event listeners at the moment that
these various attributes and other things are mapped. There are a
lot of ways to catch such events, but the most easy one that was
added mostly for this purpose is known as the "attribute_instrument"
event, which is called whenever a new class attribute becomes part
of a SQLAlchemy mapping. This is a good place where you can check
how this attribute is mapped and make decisions about what kind of
validation it should have. Two examples are provided, one which
illustrates how to apply a listener to all the events associated
with a base class, that is at
http://docs.sqlalchemy.org/en/rel_1_0/_modules/examples/custom_attributes/listen_for_events.html,
and there's also a recipe which I think is nicer that sets up
validators based on the column types, such as your Integer here,
that is at
https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/ValidateOnType.
--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it,
send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.