How to use flask_tryton


I was trying to make a flask app that send info to tryton using flask_tryton.

The code is the follow:

from flask import render_template, flash, redirect, url_for, session
from app import app
from app import tryton 

Party = tryton.pool.get('')

def default_context():
	return User.get_preferences(context_only=True) 

@app.route('/', methods=['GET', 'POST'])
def index():
    form = LoginForm() 
	if form.validate_on_submit():
		firstname =
		party, = Party.create([{
			'name': firstname,

I have already setting these variables:

    TRYTON_DATABASE: the Tryton’s database to connect.
    TRYTON_USER: the Tryton user id to use, by default 0 (aka root).
    TRYTON_CONFIG: the optional path to the Tryton’s configuration.

But when I send the info I receive this error:

  File "/testlib/python3.5/site-packages/flask/", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/testlib/python3.5/site-packages/flask/", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/testlib/python3.5/site-packages/flask/", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/testlib/python3.5/site-packages/flask/", line 35, in reraise
    raise value
  File "/testlib/python3.5/site-packages/flask/", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/testlib/python3.5/site-packages/flask/", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/testlib/python3.5/site-packages/", line 97, in wrapper
    with Transaction().start(database, 0):
  File "/testlib/python3.5/site-packages/trytond/", line 96, in start
  File "/testlib/python3.5/site-packages/trytond/backend/postgresql/", line 158, in get_connection
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/", line 84, in check_async_
    return func(self, *args, **kwargs)
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/", line 273, in set_isolation_level
    prev = self.isolation_level
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/", line 43, in check_closed_
    return func(self, *args, **kwargs)
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/", line 265, in isolation_level
    name = self._get_guc('default_transaction_isolation')
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/", line 234, in _get_guc
    raise exceptions.OperationalError("can't fetch %s" % name)
psycopg2cffi._impl.exceptions.OperationalError: can't fetch default_transaction_isolation

I’m not sure what thing I miss something but I based the code in

Thanks in advance!

Do you have the same issue with psycopg2 (and not psycopg2cffi) ?

Well, I don’t use psycopg2 'cause can run trytond with it, I only have psycopg2cffi for database usage, it works fine, so I’m not sure if I need some special config with flask_tryton?

There is no special configuration regarding the database for flask_tryton except the variables you’re setting. That’s why the most probable issue look like psycopg2cffi, it would be interesting anyway to understand why you can’t run tryton with psycopg2 (are you using pypy ?) and it would help to rule out an eventual issue in flask_tryton.

I use ubuntu or debian for diffent apps, but in no one I can use psycopg2, at the moment I don’t have an instance running without psycopg2 but the most common trouble log is with isolation transaction.I use pypi to to install flask_tryton, I’m trying to migrate to docker but first I need to see that it works fine in a tradicional enviroment.

I couldn’t not find the code of flask_tryton but I create one on, so maybe I miss some config according to

Thanks for your help!

I am sorry but I don’t understand a word you say. Do you or do you not use psycopg2 ?

Also pypi ≠ pypy. The first one is the python package index, the second one is an implementation of the python interpreter in python.

Well, I have install psycopg and psycopg2cffi 'cause without it tryton does not run, I don’t know why. Well, I don’t use pypy.

This is really strange it shouldn’t happen. You have messed up something in your installation.

I need to add the follow lines to the route, after it works well.
@app.route(’/’, methods=[‘GET’, ‘POST’])

After some time, the previous message make it works, but sometimes the app downs, so I uninstall psycopg2cffi and try the follow post, it works fine for me now.

# the fix
lazy = true
lazy-apps = true
1 Like

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