D
D
Dmitry Shnyrev2014-12-20 15:51:21
Flask
Dmitry Shnyrev, 2014-12-20 15:51:21

How to get old attribute values ​​in SqlAlchemy in event?

Hello.
Can you tell me the simplest solution how to get the previous attribute values ​​in the before_update or after_update event? I can not find normal examples on the Internet.
I know that the documentation uses the set event for an attribute for this, but this is not suitable for the situation (or I didn’t understand how to use it) if I need to check how several attributes have changed at once, and not just one, before inserting into the database. In Rails, for example, you can use <attribute>_changed? <attribute>_was. How this is done in SQLAlchemy I do not find something.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shnyrev, 2014-12-22
@dmnBrest

The question was solved a little around - I do the check directly in the controller (business logic). In 99% of cases, at the input I have a form with new data and the model itself with data from the database. Here I compare them. I did not think that this is such a problem for this ORM. It turns out that with SQLAlchemy we have a thick controller and a thin model as an output.
UPD: finally found the answer. In general, for the first time I meet in ORM that this issue was so launched.
hist = attributes.get_history(self, 'elements')
we get the following object
added=['new value'], unchanged=(), deleted=[u'previous value']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question