Skip to Content
Menú
This question has been flagged

Hi all!

I have a model with a field that I want to constantly change.


This my model:

class TestModel(models.Model):
_name = 'test'

device = fields.Char(string="Device")

time = fields.Char()
lastTime = fields.Char()

In the controller I get the record id and the new lastTime value:

 def edit(self, **rec):
try:
        alert_last_time = request.env['test'].search([], limit=1)
alert_last_time.lastTime = rec['lastTime']
alert_last_time.write({'lastTime': rec['lastTime']})
except KeyError:
return {'success': False,
'message': 'KeyError'}
else:
return {'success': True,
'message': 'Record was successfully updated'}

If I create several records and try to change them, the first created record changes, but how to make the record whose id I have specified change, I do not yet understand


Avatar
Descartar
Bonjour, Je serai de retour le 2 septembre. Bien cordialement
Best Answer

Hii,

Update your edit() method like this:
@http.route('/your/route', type='json', auth='public')  # adjust route details

def edit(self, **rec):

    try:

        record_id = int(rec['id'])  # make sure 'id' is passed in JSON body

        new_last_time = rec['lastTime']


        alert_last_time = request.env['test'].browse(record_id)


        if not alert_last_time.exists():

            return {'success': False, 'message': 'Record not found'}


        alert_last_time.write({'lastTime': new_last_time})

       

    except KeyError:

        return {'success': False, 'message': 'Missing "id" or "lastTime"'}

    except Exception as e:

        return {'success': False, 'message': str(e)}

    else:

        return {'success': True, 'message': 'Record was successfully updated'}


JSON Payload Example

Make sure your frontend or API call sends this:

{ "id": 3, "lastTime": "2025-07-16 17:00:00" }

i hope it is usefull

Avatar
Descartar
Best Answer

Hi,


In your program you are using the search method on your "test" module and limiting it by 1


python

alert_last_time = request.env['test'].search([ ], limit = 1)


if you want your program to work on a specific id, instead of using the "search" method use the "browse" method.


python

alert_last_time = request.env['test'].browse(your_id or ids)

your_id or ids" can be a single variable id or a list of ids


Hope it helps

Avatar
Descartar
Best Answer
tobj = self.env['test'].search([ ], limit = 1) 
tobj.write({'lastTime': self.lastTime})


Avatar
Descartar
Related Posts Respostes Vistes Activitat
2
de juny 24
2496
1
d’abr. 24
1608
1
de maig 23
3479
5
de febr. 24
17819
2
de gen. 24
1431