简体   繁体   中英

Problems overwriting a model field in Python in Odoo?

I have created a module which modifies other one (named base ). In the module base there is the res.partner model, and in this model there is the field birthdate :

_columns = {
...
    'birthdate': fields.char('Birthdate'),
...
}

What I do in my module is to overwrite this field to make it of type Date :

birthdate = fields.Date('Birthdate')

Everything seems OK, but, after updating the Odoo server, the data introduced in that column dissapears from the view, and when I check the database, I find that the column birthdate is being duplicated with other names like birthdate_moved0 , birthdate_moved1 , birthdate_moved2 , etc... (and half of them are of type char and the other half of type date ). The values stored in birthdate are being moved to these other columns (that's the reason bacause I can't see the data in the view, since in the form only birthdate is being shown).

However, I was able to overwrite several fields through Python. But this duplication problem happened me with the field birthdate and the field function of the model res.partner .

I can't come to a conclussion. Can anyone help me here, please? Thank you in advance!

You should name your "new" field 'birth_date' or 'dob' or anything other than 'birthday' just to avoid changing existing field data type. In next step you can copy values from current 'birthday' field to new one (through postgresql).

Finally, a co-worker shown me the solution:

It's not about Odoo, it's due to PostgreSQL. Generally, in PostgreSQL, is not possible to alter the data type of a column (even when this is empty), except for some cases, like for example:

  • From integer to char : because the casting to char is possible. Therefore, in Odoo, when you change the data type of a field.Date, a field.Integer, a field.Many2one, etc... to a fields.Char, there is no problem. However, if you try to change a fields.Char to fields.Date or fields.Many2one, or whatever, PostgreSQL is going to duplicate the column because is not ready for that type of casting.

That's the reason because I wasn't able to change a field of type Char and transform it in a field of kind Date (my attempt with birthdate ) or Many2one (my attempt with function ). And on the other hand, I was able to overwrite a Selection field (actually, in PostgreSQL is convert a Char into another Char).

So in conclusion: If you are going to change the type of data of a field, check if the final kind of data is char (fields.Char, fields.Selection, etc...) or other possible casting. Then you can name the new field with the same name as before. If not, you must name the new field with other name, otherwise PostgreSQL will duplicate the column with name_moved0 , name_moved1 , etc...

I hope this helps to anyone!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM