[英]Problems overwriting a model field in Python in Odoo?
I have created a module which modifies other one (named base
). 我创建了一个修改其他模块的模块(命名为
base
)。 In the module base
there is the res.partner
model, and in this model there is the field birthdate
: 在模块
base
有res.partner
模型,在该模型中有字段birthdate
:
_columns = {
...
'birthdate': fields.char('Birthdate'),
...
}
What I do in my module is to overwrite this field to make it of type Date
: 我在模块中要做的是覆盖此字段,使其类型为
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 ). 一切似乎都很好,但是在更新Odoo服务器之后,该列中引入的数据从视图中消失了,当我检查数据库时,我发现该列的
birthdate
正在与其他名称(如birthdate_moved0
, birthdate_moved1
, birthdate_moved2
等)重复。 ...(其中一半为char类型,另一半为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). birthdate
中存储的值将移动到其他这些列(这是因为我无法在视图中看到数据,因为在表单中仅显示了birthdate
)。
However, I was able to overwrite several fields through Python. 但是,我能够通过Python覆盖多个字段。 But this duplication problem happened me with the field
birthdate
and the field function
of the model res.partner
. 但是这个重复问题发生在字段
birthdate
和模型res.partner
的字段function
上。
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. 您应该将“新”字段的名称命名为“ birth_date”或“ dob”或“ birthday”以外的名称,以避免更改现有的字段数据类型。 In next step you can copy values from current 'birthday' field to new one (through postgresql).
在下一步中,您可以将值从当前“生日”字段复制到新字段(通过postgresql)。
Finally, a co-worker shown me the solution: 最后,一位同事向我展示了解决方案:
It's not about Odoo, it's due to PostgreSQL. 与Odoo无关,这是由于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:
通常,在PostgreSQL中,除某些情况外,无法更改列的数据类型(即使该列为空),例如:
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 ). 这是因为我无法更改Char类型的字段,而无法将其转换为Date(我的尝试带有birthdate )或Many2one(我的尝试带有function )类型的字段。 And on the other hand, I was able to overwrite a Selection field (actually, in PostgreSQL is convert a Char into another Char).
另一方面,我能够覆盖一个Selection字段(实际上,在PostgreSQL中是将一个Char转换为另一个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. 因此得出结论:如果要更改字段的数据类型,请检查最终的数据类型是否为char(fields.Char,fields.Selection等)或其他可能的强制转换。 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...
如果不是,则必须使用其他名称来命名新字段,否则PostgreSQL将使用name_moved0 , name_moved1等复制该列。
I hope this helps to anyone!! 我希望这对任何人都有帮助!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.