get last item on One2Many

76 views
Skip to first unread message

Francisco Maria Moyano Casco

unread,
May 27, 2016, 2:55:05 PM5/27/16
to tryton
Hello.
          I need to use the last element of a One2Many field. Is there any function to do that?

          Thanks
                  Francisco

Cédric Krier

unread,
May 27, 2016, 3:10:04 PM5/27/16
to tryton
On 2016-05-27 11:04, Francisco Maria Moyano Casco wrote:
> Hello.
> I need to use the last element of a One2Many field. Is there any
> function to do that?

You can use negative index -1
See point 3 of
https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange

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

Francisco Maria Moyano Casco

unread,
May 27, 2016, 5:05:04 PM5/27/16
to tryton
Yes, I tried to, but I got an error.

Maybe is because I'm using Tryton-3.4?

Cédric Krier

unread,
May 27, 2016, 5:20:03 PM5/27/16
to tryton
On 2016-05-27 12:28, Francisco Maria Moyano Casco wrote:
> Yes, I tried to, but I got an error.

It is not clear what you have tried.
Also if you want us to help you, you must provide more information.
What was the code you tried? What was the error? etc.


PS: Please don't top-post on this mailing list, see
http://groups.tryton.org/netiquette

Francisco Maria Moyano Casco

unread,
May 29, 2016, 2:10:03 AM5/29/16
to tryton

Sorry, my last message was rejected for top-posting.

I already solved it aided by friends from Silix.


It is not clear what you have tried.
Also if you want us to help you, you must provide more information.
What was the code you tried? What was the error? etc.

I tried to used negative index -1

(....)
    housing_conditions = fields.One2Many('gnuhealth.housing','du',
            'Housing Conditions')
   
    critical_housing = fields.Function(fields.Boolean('Vivienda Critica'),
        'get_critical_housing',searcher='search_critical_housing')

    @classmethod
    def search_critical_housing(cls,name,clause):
      res = []
      value = clause[2]
      if(cls.housing_conditions._get_size()!=0):
         this_one = cls.housing_conditions[-1].critical_housing
         res.append(('this_one',clause[1],value))
      return res
(....)

And gave me this error:

(...)
 File "/trytond/model/modelsql.py", line 1146, in convert
    expression = field.convert_domain(domain, tables, cls)
  File "/trytond/model/fields/function.py", line 64, in convert_domain
    return getattr(Model, self.searcher)(name, domain)
  File "/trytond/modules/z_DU/health.py", line 46, in search_critical_housing
    this_one = cls.housing_conditions[-1].critical_housing
TypeError: 'One2Many' object does not support indexing
(...)


I could solved with this

    @classmethod
    def search_critical_housing(cls, name, clause):
         cursor = Transaction().cursor
         pool = Pool()
         DomiciliaryUnit = pool.get('gnuhealth.du')
         HousingConditions = pool.get('gnuhealth.housing')
         cursor.execute('SELECT du.id '
             'FROM "' + DomiciliaryUnit._table + '" du '
                 'INNER JOIN ( '
                     'SELECT DISTINCT ON (du) du, critical_housing '
                     'FROM "' + HousingConditions._table + '" '
                     'ORDER BY du, id DESC '
                 ') last_hc '
                 'ON du.id = last_hc.du '
             'WHERE last_hc.critical_housing = TRUE')
         has_critical_housing = cursor.fetchall()
         field, op, operand = clause
         if (op, operand) in (('=', True), ('!=', False)):
             return [('id', 'in', has_critical_housing)]
         elif (op, operand) in (('=', False), ('!=', True)):
             return [('id', 'not in', has_critical_housing)]
         else:
             return []


Thanks anyway :) !!!

             Francisco

 
Reply all
Reply to author
Forward
0 new messages