In views:
model = Model('some_title', 'some text')
session.add(model)
return HTTPFound(location='/ads/%s/%s' % (model.id, model.title))
So, it must redirects me to /ads/1/some_title
(if id=1), instead it redirects me to /ads/None/some_title
.
How to get an id
of this row after created db row in this particular example?
Thanks!
at the point you ask for model.id
, the new model has not yet reached the database; pyramid waits until the request handler returns before commiting the pending transaction. To get the id earlier, you must flush the session. Add:
model = Model('some_title', 'some text')
session.add(model)
session.flush()
return HTTPFound(location='/ads/%s/%s' % (model.id, model.title))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With