How to refresh the transaction in button call

Hello,
I have a button which call a subprocess inside the method, that subprocess is a python script which update the received record on the database. (using psycopg2)

I tried to re instance after the subprocess.check_output() like:

updated_invoice = Pool().get(‘account_invoice’)

But I see the old record (without the information added by the script), ¿Is there any way yo tell the ORM to refresh or query again the db?

PD: If I click the button again, the record has the new information, I guess it’s because tryton is using a new transaction
Thanks in advance.

You should create a new transaction that reads the values commited by the external proccess.

Why are you using an external script? It will be easier to call the same process in the same Tryton transaction

Sorry, How could I create the new transaction?

I’m using Invoice.search() after the subprocess call but not sure if this triggers a new transaction.

Unfortunaly the external script was there long ago with java code, so I’m planning in future integrate it to tryton, for now it’s not possible for me.

You should call transaction.new to strat a new transaction.

From there you wilm be able to perform any databae query which will see the values created on the external process.

I’m trying with this:

@classmethod
@ModelView.button
def do_attached_document(cls, invoices):
              transaction = Transaction()
              transaction.new_transaction()
              Invoice = Pool().get('account.invoice')

Which seems to work, but from tryton I get this error, I know I’m very close :sweat_smile::

Traceback (most recent call last):
File “/trytond/wsgi.py”, line 80, in dispatch_request
return endpoint(request, **request.view_args)
File “/trytond/protocols/dispatcher.py”, line 46, in rpc
request, database_name, *request.rpc_params)
File “/trytond/wsgi.py”, line 51, in auth_required
return wrapped(*args, **kwargs)
File “/trytond/protocols/wrappers.py”, line 122, in wrapper
return func(request, pool, *args, **kwargs)
File “/trytond/protocols/dispatcher.py”, line 205, in _dispatch
security.reset(pool.database_name, session, context=context)
File “/trytond/security.py”, line 134, in reset
with Transaction().start(dbname, 0, context=context):
File “/trytond/transaction.py”, line 86, in start
assert self.user is None
AssertionError

I figure it out with:

                with Transaction().new_transaction() as transaction:
                    with transaction.set_user(1):
                        Invoice = Pool().get('account.invoice')

Thank you

It is not really the Model that is linked to the transaction but you must be under the new transaction to call methods on it like search.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.