How to compare Date using Between

Hi,

I’m working with a query using Between to compare my dates, but i got this error

Traceback (most recent call last):
  File "/home/julio/Development/workspace-python/tryton/facturacion/trytond/trytond/protocols/dispatcher.py", line 181, in _dispatch
    result = rpc.result(meth(*c_args, **c_kwargs))
  File "/home/julio/Development/workspace-python/tryton/facturacion/trytond/trytond/model/modelsql.py", line 159, in wrapper
    return func(cls, *args, **kwargs)
  File "/home/julio/Development/workspace-python/tryton/facturacion/trytond/trytond/model/modelsql.py", line 674, in create
    cls._validate(sub_records)
  File "/home/julio/Development/workspace-python/tryton/facturacion/trytond/trytond/model/modelstorage.py", line 76, in wrapper
    return func(*args, **kwargs)
  File "/home/julio/Development/workspace-python/tryton/facturacion/trytond/trytond/model/modelstorage.py", line 1344, in _validate
    cls.validate(records)
  File "/home/julio/Development/workspace-python/tryton/facturacion/trytond/trytond/modules/account_invoice_py/invoice_py.py", line 143, in validate
    cursor.execute(*stamped.select(Count(Literal('*')),
  File "/home/julio/Development/workspace-python/tryton/facturacion/trytond/trytond/backend/postgresql/database.py", line 69, in execute
    cursor.execute(self, sql, args)
psycopg2.ProgrammingError: can't adapt type 'Date'

My query:

cursor.execute(*stamped.select(Count(Literal('*')), 
                                       where=(
                                            And((
                                                stamped.state.like('active'),
                                                Or((
                                                    Between(cls.begin_date,stamped.begin_date, stamped.end_date),  
                                                    Between(cls.end_date,  stamped.begin_date, stamped.end_date)
                                                ))                                                
                                            ))
                                        )                
                                    ))

All my fields are fields.Date.

I am newbie using python-sql and still don’t understand it well.

Thanks for your help…

You can not use Field instance in SQL query, you must use Column which can be retrieve from sql_column method.
Anyway I do not see why using plain SQL query when you can do the same using ModelStorage.search with the parameter count=True.

I just try with sql plain because until now that was the only way that I known. I will try to convert my dates fields to build my query.

Search method allows only And operator? Or can I use OR too?

Both see Domain — Tryton server

So I just need treat it like I do with domain parameter of fields? :thinking:

Yes, domains are used to build search filters and also field constraints.

It worked.

I used search and my code looks so clean and readable with it.

Thanks,

1 Like

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