Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
11757 Lượt xem

I want to strip the input data to remove leading and trailing spaces.

I tried this, but didn't work...

class Test(models.Model):
    _name = 'test.mymodel'
    device_id = fields.Char(string="ID", required=True).strip()

and this too didn't work...

class Test(models.Model):
_name = 'test.mymodel'
device_id_strp = fields.Char(string="ID", required=True)
    device_id = device_id_strp.strip()

please help me with a solution...

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

I don't know if this solution is the most efficient, but you could add an onchange method to the field.
In this onchange, strip the white spaces.

edit:

@api.onchange("device_id")
def onchange_device_id(self):
    self.device_id = self.device_id.strip()

Ảnh đại diện
Huỷ bỏ
Tác giả

hello Jerome... isn't there a way to do this in the back end?

+1, this is the behaviour that should probably be used. And Rizan when you trigger an on_change it will trigger the Python which is your back-end in essence. Another way is to remove all whitespaces the moment you hit the save button.

It is backend if you declare a method in your model. Not a javascript onchange :) in your model, do something like: @api.onchange("device_id") def onchange_device_id(self): self.device_id = self.device_id.strip()

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 5 24
3626
3
thg 9 23
4367
0
thg 7 25
1498
2
thg 11 24
6439
2
thg 10 24
3446