Odoo Help


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.


How to strip (remove white spaces) inputs of forms before adding to database?

Rizan Zaky
on 11/13/15, 3:30 AM 360 views

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...

Jerome Guerriat
On 11/13/15, 3:46 AM

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.


def onchange_device_id(self):
    self.device_id = self.device_id.strip()

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

Rizan Zaky
on 11/13/15, 3:50 AM

+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.

on 11/13/15, 4:22 AM

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()

Niboo SPRL, Jerome Guerriat
on 11/13/15, 4:24 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

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

Question tools

2 follower(s)


Asked: 11/13/15, 3:30 AM
Seen: 360 times
Last updated: 11/13/15, 4:36 AM