Pyramid: how to get id of just created db string?

In the views:

model = Model('some_title', 'some text') session.add(model) return HTTPFound(location='/ads/%s/%s' % (model.id, model.title)) 

Therefore, it should redirect me to /ads/1/some_title (if id = 1), instead it redirects me to /ads/None/some_title .

How to get id this line after db line created in this particular example?

Thanks!

+4
source share
1 answer

at the point you request model.id , the new model has not yet reached the database; The pyramid waits until the request handler returns before it makes a pending transaction. To get the identifier earlier, you must clear the session. Add:

 model = Model('some_title', 'some text') session.add(model) session.flush() return HTTPFound(location='/ads/%s/%s' % (model.id, model.title)) 
+11
source

Source: https://habr.com/ru/post/1389827/


All Articles