This question has been flagged
2 Replies
3724 Views

Ex.

class myClass(models.Model):
_name = 'my.class'
name = fields.Char(string='Title', required=True)
description = fields.Char(string='description', required=True)
model_name = fields.Many2one('ir.model', string="Model", required=True)
model_field = fields.Many2one('ir.model.fields', string='Field', domain="[('model_id', '=',model_name))

How i get date strings from M2O fields:  model_name, model_field.

Let says, in a @api.onchange decorator, thanks in advance.

@api.depends('time', 'model_field')
@api.onchange('model_field')
def _my_funt(self):
    code...
Avatar
Discard
Author Best Answer

Hi, this is my code:

class myClass(models.Model):
_name = 'my.class'

name = fields.Char(string='Title', required=True)
description = fields.Char(string='description', required=True)
model_name = fields.Many2one('ir.model', string="Model", required=True)
model_field = fields.Many2one('ir.model.fields', string='Field', domain="[('model_id', '=',model_name),('ttype', 'in', ['datetime','date'])]", required=True)
    date_set = fields.Date(string='Select Date')
    date_from = fields.Date(string="Start Date")
    date_to = fields.Date(string="End Date")
    time = fields.Integer("Value")
    time_left = fields.Char(compute="_calcule")

@api.onchange('model_field')
    def _calcule(self):
        self.time_left = self.model_name.model_field


i tried:

self.time_left = self.model_name.model_field

 But it showing a error:

AttributeError: 'ir.model' object has no attribute 'model_field'

thanks

Avatar
Discard
Author

Hi, DHA Medic model_field is the name of de the many2one field in my.class

I would like to understand, how i can access the value from this field, that in this case is a date.

Im just getting the record(xxxx,) instead the value of that record.

Thanks

@api.onchange('model_field')

def _calcule(self):

if self.model_field:

mid = self.model_field.id

self.time_left = self.env['ir.model'].search([('field_id', '=', mid)])

#you must debug this code, i have not test it

pastebin.com/i20zDMDL

Author

I have tested it.

Im getting ir.model(317,) on time_left field.

Thanks

Best Answer

self.model_name.your_field_want_to_get

Avatar
Discard