KAKA RECARDO
KAKA RECARDO

Reputation: 115

How to fill a field by date now from a click of a button

I want to fill a field by date now from a click of a button of the action

class sale_inheritd(models.Model):
_inherit = 'sale.order'
@api.multi
def action_sale_temporary(self):
    for order in self:
        self.env['project.project'].search([('project_id', '=', 'related_project_id')]).write({'temporary_reception_date':datetime.date.today()})
        order.write({'state': 'temporary'})

What is the problem with this function?

Upvotes: 2

Views: 618

Answers (2)

Charif DZ
Charif DZ

Reputation: 14721

I didn't understand the domain that you have passed to search method but if you want to fill a field with type Date:

   .write({'temporary_reception_date':fields.Date.today()})

and for Datetime field:

   .write({'temporary_reception_date':fields.Datetime.now()})

Note: and don't use self to access a field inside the loop exm: self.related_project_id use order.related_project_id instead or you most likely will have Singleton Error

EDITS: as @CZoellner said it's better to fields.Date.context_today(self) because that will prevent problems with user timezones

Upvotes: 2

Amal
Amal

Reputation: 234

From your question it is not clear what are you trying to accomplish. Your search method you search for project_id[looks like many2one field] equals to a string.

If it is many2one field then pass id to get correct result. Also make sure self.env['project.project'].search([('project_id', '=', 'related_project_id')]) returns a single record only else it will lead to singleton error.

If there is only one record returned by search method then there is no issue in writing to the record like you did.

Upvotes: 1

Related Questions