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

Hi, does anyone know if it's possible to extend a python field definition so that we can change the string attribute?

I don't want to redefine the entire field just as it appears in base odoo. I just want to change one attribute of the field. Something like:

book_value=fields.Monetary(string='Net Book Value')
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi Bill, if you need to extend the field to change only one attribute you can actually define the field again and only change that attribute, the ORM will keep every other attribute and change the one you re-defined. 


from odoo import models, fields

class AccountAsset(models.Model):

_name = 'account.asset'
_description = 'Asset/Revenue Recognition'
_inherit = ['mail.thread', 'mail.activity.mixin']

book_value = fields.Monetary(string='Book Value', readonly=True, compute='_compute_book_value', store=True)


-- Inheritance 1: Module 1 that depends on account_asset --

class AccountAsset(models.Model):
_inherit = 'account.asset'

book_value = fields.Monetary(string='Custom string 1')

-- Inheritance 2: Module 2 that depends on module 1 (or installed after) --

class AccountAsset(models.Model):
_inherit = 'account.asset'

book_value = fields.Monetary(string='Custom string 2')

Outcome string: "Custom string 2"

It takes the last attribute defined in the AccountAsset class. ORM will automatically keep following attributes attached to the field:

  • readonly=True, 
  • compute='_compute_book_value', 
  • store=True 

Note: other attributes might still change if they are re-defined by other modules, or they also might be shown differently in specific views, if there is some specific 'string' attribute at view-level (XML view definition).

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

I just tried this and it seems to work that I can just include the attributes that I'm changing. Thanks!

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 6 25
1181
3
thg 7 25
2916
1
thg 5 25
1101
1
thg 5 25
1373
4
thg 5 25
2495