Replacement for Transaction.cursor

71 views
Skip to first unread message

Marko Randjelovic

unread,
Jul 30, 2016, 11:15:24 AM7/30/16
to tryton
Hi all,

I have a piece of code that uses cursor, but looks like cursor is removed from Trytond 4. When I use this code inside of Tryton 4, I get this error:

Traceback (most recent call last):
  File "/trytond/wsgi.py", line 47, in dispatch_request
    return endpoint(request, **request.view_args)
  File "/trytond/protocols/dispatcher.py", line 59, in rpc
    request, database_name, *request.params)
  File "/trytond/wsgi.py", line 39, in auth_required
    return wrapped(*args, **kwargs)
  File "/trytond/protocols/dispatcher.py", line 40, in wrapper
    return func(request, pool, *args, **kwargs)
  File "/trytond/protocols/dispatcher.py", line 201, in _dispatch
    result = rpc.result(meth(*c_args, **c_kwargs))
  File "/trytond/model/modelsql.py", line 762, in read
    getter_results = field.get(ids, cls, field_list, values=result)
  File "/trytond/model/fields/function.py", line 92, in get
    return dict((name, call(name)) for name in names)
  File "/trytond/model/fields/function.py", line 92, in <genexpr>
    return dict((name, call(name)) for name in names)
  File "/trytond/model/fields/function.py", line 84, in call
    return method(records, name)
  File "/trytond/modules/training/opportunity.py", line 92, in get_description_length
    cursor = Transaction().cursor
AttributeError: 'Transaction' object has no attribute 'cursor'


This is the code:

    @classmethod
    def get_description_length(cls, opportunities, name):
        cursor = Transaction().cursor

        opportunity = cls.__table__()
        query = opportunity.select(
            opportunity.id, CharLength(opportunity.description),
            where=opportunity.id.in_([o.id for o in opportunities]))
        cursor.execute(*query)
        return {id: length for id, length in cursor.fetchall()}


We used cursor to execute query, now obviously there should be some replacement. I tried to look at the source to find out what that could be, but there were too many problems.

The class Transaction as defined in trytond/transaction.py indeed contained attribute cursor and in new version this attribute is missing. I need a replacement way to execute the query as can be seen from the provided excerpt of my code.

Please help.

Cédric Krier

unread,
Jul 30, 2016, 3:05:03 PM7/30/16
to tryton
On 2016-07-30 08:15, Marko Randjelovic wrote:
> The class Transaction as defined in trytond/transaction.py indeed contained
> attribute cursor and in new version this attribute is missing. I need a
> replacement way to execute the query as can be seen from the provided
> excerpt of my code.

The cursor is accessible from the connection:
http://doc.tryton.org/4.0/trytond/doc/ref/transaction.html?highlight=cursor#trytond.transaction.Transaction.connection

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Cédric Krier

unread,
Jul 30, 2016, 3:20:03 PM7/30/16
to tryton
On 2016-07-30 21:03, Cédric Krier wrote:
> On 2016-07-30 08:15, Marko Randjelovic wrote:
> > The class Transaction as defined in trytond/transaction.py indeed contained
> > attribute cursor and in new version this attribute is missing. I need a
> > replacement way to execute the query as can be seen from the provided
> > excerpt of my code.
>
> The cursor is accessible from the connection:
> http://doc.tryton.org/4.0/trytond/doc/ref/transaction.html?highlight=cursor#trytond.transaction.Transaction.connection

By the way, I just updated the Transaction doc to be correct and
complete: https://bugs.tryton.org/issue5759

Marko Randjelovic

unread,
Jul 30, 2016, 4:03:44 PM7/30/16
to tryton


On Saturday, July 30, 2016 at 9:20:03 PM UTC+2, Cédric Krier wrote:
On 2016-07-30 21:03, Cédric Krier wrote:
> On 2016-07-30 08:15, Marko Randjelovic wrote:
> > The class Transaction as defined in trytond/transaction.py indeed contained
> > attribute cursor and in new version this attribute is missing. I need a
> > replacement way to execute the query as can be seen from the provided
> > excerpt of my code.
>
> The cursor is accessible from the connection:
> http://doc.tryton.org/4.0/trytond/doc/ref/transaction.html?highlight=cursor#trytond.transaction.Transaction.connection

By the way, I just updated the Transaction doc to be correct and
complete: https://bugs.tryton.org/issue5759

I'm not sure the documentation is correct. According to documentation, there is cursor attribute of Transaction as a method, but if you write in code Transaction().cursor(), you will get error that there is not attribute cursor.

Regards,
Marko
 
Reply all
Reply to author
Forward
0 new messages