Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to store the date when the state of an object was changed? [Closed]

By
Anabela Damas
on 7/12/13, 3:18 PM 2,214 views

The Question has been closed

by
Anabela Damas
on 07/15/2013 09:01:30

Hi,

I need to save the datetime when someone changes the Invoice State and I also need to store the user that made that change.

So I need that whenever someone changes the state field that two new fields ('state_change_date', 'state_change_user') to be updated because I just need to store the last change.

I tryed to figure it out how to do this, but I just found one way that I don't like that it is to look in all code an see where the state is changed and inherit all of this functions and change my new fields...

If someone knows a better way I would appreciate =)

After the answer from @nazarii I tryed several ways , and I also have several errors... : I've defined the field here:

_columns = {
        'invoice_status_date': fields.function(_get_invoice_status_date, method=True, type="datetime", store={'account.invoice': (lambda self, cr, uid, ids, ctx={}: ids, ['state'], 10),}, string="State change date"),
    }

1st way :

def _get_invoice_status_date(self, cr, uid, ids, state, args, context=None): 
    res = {}
    res = {
        'invoice_status_date' : data_now
        }
    return res

1st error

File "/opt/openerp-7.0-20130516-231144/openerp/sql_db.py", line 226, in execute res = self._obj.execute(query, params) DataError: invalid input syntax for integer: "invoice_status_date"  LINE 1: ...ice_status_date"='2013-07-15 09:17:09' where id = 'invoice_s...

2nd way:

def _get_invoice_status_date(self, cr, uid, ids, state, args, context=None): 
     res = {}
     for invoice in self.browse(cr, uid, ids, context=context):
        res[invoice.id] = {
             'invoice_status_date' : fields.datetime.now()
              }
    return res

2nd Error:

DataError: invalid input syntax for type timestamp: "{'invoice_status_date': '2013-07-15 09:23:16'}"

LINE 1: ...pdate "account_invoice" set "invoice_status_date"='{''invoic...

3rd way

def _get_invoice_status_date(self, cr, uid, ids, state, args, context=None): 
      res = {}
      for invoice in self.browse(cr, uid, ids, context=context):
          res[invoice.id]['invoice_status_date'] = fields.datetime.now()
      return res

3rd Error

  File "/opt/openerp-7.0-20130516-231144/openerp/addons/l10n_pt_saft7/l10n_pt_saft7.py", line 149, in _get_invoice_status_date
res[invoice.id]['invoice_status_date'] = fields.datetime.now()
KeyError: 12

What am I missing ?

Thanks

0
nazarii
On 7/12/13, 4:49 PM

I see two solutions for this issue:

  1. To use write method and check whether state field is in it's values, if so - update values with you date field and it's new value;
  2. Make this date field as functional with store parameter defined, so it will recalculate when state field has been changed. This will looks like:

    state_change_date: fields.function(_get_state_change_date, method=True, type="date", store={'your_model': (lambda self, cr, uid, ids, ctx={}: ids, ['state'], 10),}, string="State change date")

I tried to do like you told me to , but I couldn't. I've updated my answer.... Do you know what I doing wrong ? Thanks

Versão Integral, Anabela Damas
on 7/15/13, 8:11 AM

Your _get_invoice_status_date is returning wrong value, answer can be found in first answer here http://help.openerp.com/question/24366/functional-fields-in-openerp-convert-amount-to-word-string/#24369

nazarii
on 7/15/13, 8:31 AM

Thanks, I fixed like this:

def _get_invoice_status_date(self, cr, uid, ids, field_name, arg, context=None):

    result = {}
    for invoice in self.browse(cr, uid, ids, context=context):
        result[invoice.id] = fields.datetime.now()

    return result
Versão Integral, Anabela Damas
on 7/15/13, 9:00 AM

Hi, I need the same thing but instead field date I need know the user that change the state. I tried with uid but in this case the uid is always 1, the administrator user.

Luís
on 7/15/13, 2:03 PM

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 7/12/13, 3:18 PM
Seen: 2214 times
Last updated: 3/16/15, 8:10 AM