我們有如下一個Selection類型的字段:
class ZeroneBook(models.Model):
_name = "zerone.book"
_description = "Zerone Books"
binding_type = fields.Selection([
("common", "普通"),
("hardcover", "精裝")
], string="裝幀類型", index=True, default='common')
當我們執行輸出語句
print(self.binding_type)
這時候,會輸出:common或者hardcover,而不是他的顯示值:普通或精裝。
但在有些時候,我們需要的就是他的顯示值,比如:我們在其他的模型中,需要用zerone.book模型中binding_type的顯示值爲當前模型的Html字段做一個拼接信息作爲默認值。這時候,我們可以通過下面的代碼,去實現:
class ZeroneBook(models.Model):
_name = "zerone.other"
_description = "Zerone Others"
binding_type = fields.Selection([
("common", "普通"),
("hardcover", "精裝")
], string="裝幀類型", index=True, default='common')
bind_info = fields.Html(string="裝幀信息")
def _compute_binding_info(self):
time = "2020-03-10 12:04"
type_dict = dict(self.env["zerone.book"].fields_get(allfields=['binding_type'])['binding_type']['selection'])
print(type) # {"common":"普通","hardcover":"精裝"}
return "時間:" + time + "類型:" + type_dict[self.binding_type]