Pass context from flask to tryton


I am working in a flask app that pass some arguments to tryton.

I imported Transaction in flask routes file. Something like described here:

from trytond.transaction import Transaction

@bp.route('/course/',methods=['GET', 'POST'])
def course():
    with Transaction().set_context(courses=courses):
          # do something

But it doesn’t pass the arguments to tryton. Do I need to use @tryton.transaction() or something similar?

Thanks in advance!

Flask tryton accepts an optional context parameter. It can be set with a dict (or a method that returns a dict). This values will be set on the transaction context.

I don’t know how to use it correctly to pass context with @tryton.transaction() because it is used like a decorator before define the function with the route.

How it is used inside the function and update the context?

Maybe you should get “courses” before to pass to transaction:

def course():
    data = request.get_json()
    with Transaction().set_context(courses=data['courses']):

I can get courses variable inside, but not to pass it, it does not work.

Maybe create an aditional @bp.route with the decorator and the context param. I’m not sure if it is the best way.

Why don’t you use:

from flask_tryton import Tryton, Transaction

I tried it too, but doesn’t work neither…

What does not work? Which error do you have? What are you trying to achieve?
If you don’t give more details it’s not possible to help you.


I have the follow route in flask. Every lesson has a course, and need to pass it to tryton.

#from trytond.transaction import Transaction #doesn't workd
from flask_tryton import Transaction #doesn't work neither

@bp.route('/<base>/<slug>/',methods=['GET', 'POST'])
def lesson(base, slug):
    if base and slug:
        lessons =[
        if len(lessons)==1:
            user = User(current_user)
            defaults = {}
            defaults['lessons'] = lessons
            defaults['course'] = base
            defaults['user'] = user
            defaults['form'] = form

            if len(comments)>0:
                defaults['comments'] = comments
                defaults['comments'] = []

            courses = []
            #with Transaction().set_context(context={'courses':courses}) #doesn't work neither
            with Transaction().set_context(courses=courses):
                if lessons[0].membership_type == 'free':
                    return render_template('course/lesson.html', **defaults)
                elif lessons[0].membership_type == 'premium' and user.active_membership:
                    return render_template('course/lesson.html', **defaults)
                    return redirect(url_for('course.enroll',
    return render_template('404.html',user=user), 404

In tryton, I need to determine if an user can access or not the course.

    def get_active_membership(cls, users, name):
        pool = Pool()
        User = pool.get('web.user')
        Subscription = pool.get('sale.subscription')
        transaction = Transaction().context
        result = {}
        for user in users:
            if not
                result[] = False
                return result

            courses = transaction.get('courses') # These always is None

But I can not get the context of the route.

Thanks in advance!

An Model instance keep a copy of the context that was used when it was instantiate. So if you want to change the context of an instance, you must re-instantiate below the new context.

So, I need to create a new route in flask? Or how to re-instantiate the context?

You must re-instantiate the instance not the context.

I’m not sure how to re-instantiate the instance? Is it on Flask or in Tryton?

In you case you could just do:

with Transaction().set_context(courses=courses):
   defaults['lessons'] = Lesson.browse(lessons)

Thanks for all your help! It works very well!

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