1
votes

is there a way to display date field in different format on the view of odoo/openerp ? I tried using function fields as the follow lines:

     def _format_date(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for line in self.browse(cr, uid, ids, context=context):
            res[line.id]= {
                           'date_fin':'',
                           }
            print line.id,'date ',str(line.date).replace(',','.')
             if line.date:
                 res[line.id]['date_fin']=line.date #.split()[0]
        return res

    _columns={ 
'date_fin': fields.function( _format_date,method=True, string='date fin',
            store = {
                'stock.move': (lambda self, cr, uid, ids, c={}: ids, ['sale_price_unit','product_uom_qty'], 10),
            },
        ),
        }

I get this error:

    2015-03-11 09:35:13,539 7075 ERROR db_name openerp.http: Exception during JSON request handling.
Traceback (most recent call last):
  File "/opt/odoo/odoo/openerp/http.py", line 518, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/opt/odoo/odoo/openerp/http.py", line 539, in dispatch
    result = self._call_function(**self.params)
...
  File "/opt/odoo/odoo/openerp/models.py", line 5237, in <dictcomp>
    if name in fields
  File "/opt/odoo/odoo/openerp/fields.py", line 1028, in convert_to_cache
    return float_round(float(value or 0.0), precision_digits=self.digits[1])
TypeError: float() argument must be a string or a number

do you have any idea of this error

2

2 Answers

1
votes

In the Lang settings, you can change the date format for whole system for a specific language.

But if you want to format date only for a specific fields, you can use a computed fields instead of a related fields. In the computation method, returns the date in the good format.

You can define you computed field like this :

def _get_date_fin(self):
    self.date_fin = self.date.strftime('%d-%m-%Y')

_columns={
    'date_fin': fields.char(string='datee', size=64, readonly=True, compute=_get_date_fin),
    }
1
votes

I solved as :

   def _format_date(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for line in self.browse(cr, uid, ids, context=context):
            print line.id,'date ',str(line.date).replace(',','.')
            if line.date:
                res[line.id]=line.date.split()[0]
        return res
   _columns={       
    'date_fin': fields.function( _format_date,method=True, string='date fin',type='date',
        store = {
            'stock.move': (lambda self, cr, uid, ids, c={}: ids, ['sale_price_unit','product_uom_qty'], 10),
        },
    ),
    }