ORM之字段屬性對象Field


字段屬性對象Field

Model的字段屬性對應數據表中的相應字段,pg表中不同的字段有不同類型,Odoo也為其封裝了相應的類型對象

Field類型的屬性:

  • type = None 字段類型
  • relational = False 是否是關聯字段
  • translate = False 字段是否翻譯
  • column_type = None 數據庫字段類型
  • column_format = '%s' 字段提示信息
  • _slots = {
    'args': EMPTY_DICT, # init()的設置參數
    '_attrs': EMPTY_DICT, # the field's non-slot attributes
    '_module': None, # 字段的模塊名稱
    '_setup_done': None, # 字段設置狀態: None, 'base' or 'full'
    'automatic': False, # 是否是自動創建的字段 ("magic" field)
    'inherited': False, # 是否是繼承的字段 (_inherits)
    'name': None, # 字段名稱
    'model_name': None, # 字段所在的模型名稱
    'comodel_name': None, # 關聯字段的模型名稱
    'store': True, # 字段值是否保存到數據庫
    'index': False, # 是否在數據庫中為字段添加索引
    'manual': False, # 是否是自定義字段
    'copy': True, # 字段值是否可以復制
    'depends': (), # 字段的依賴集合
    'recursive': False, # 是否自己依賴自己
    'compute': None, # 計算字段的計算方法
    'compute_sudo': False, # 字段是否以admin重新計算
    'inverse': None, # inverse(recs) inverses field on recs
    'search': None, # search(recs, operator, value) searches on self
    'related': None, # 依賴字段的名稱
    'related_sudo': True, # whether related fields should be read as admin
    'company_dependent': False, # whether self is company-dependent (property field)
    'sparse': None, # the name of the corresponding serialized field, if any
    'default': None, # 字段默認值
    'string': None, # 字段說明
    'help': None, # 字段提示
    'readonly': False, # 是否只讀
    'required': False, # 是否必填字段
    'states': None, # set readonly and required depending on state
    'groups': None, # csv list of group xml ids
    'change_default': False, # whether the field may trigger a "user-onchange"
    'deprecated': None, # whether the field is deprecated
    'related_field': None, # corresponding related field
    'group_operator': None, # 能夠執行聚合的操作
    'group_expand': None, # name of method to expand groups in read_group()
    'prefetch': True, # whether the field is prefetched
    }

Odoo封裝的字段對象主要有:

  • Boolean
  • Integer
  • Float
  • Monetary
  • Char
  • Text
  • Html
  • Date
  • Datetime
  • Binary
  • Selection
  • Reference
  • Many2one
  • One2many
  • Many2many

Boolean

Boolean字段的type值為boolean;column_type值為('bool', 'bool')
Boolean對應pg的字段類型為boolean
例:
active = fields.Boolean(default=True, help=u"設置字段是否有效")

Integer

Integer字段的type值為integer;column_type值為('int4', 'int4')
_slots = { 'group_operator': 'sum', }
Integer對應pg的字段類型為4個字節的integer,並且能執行sum的聚合操作
例:
sequence = fields.Integer(string=u'序號', default=1)

Float

Float字段type值為float;column_type值為('numeric', 'numeric')或('float8', 'double precision')
_slots = {'_digits': None, 'group_operator': 'sum',}
Float對應pg的字段類型為8個字節的double或變長的numeric,並且能執行sum聚合操作
例:
product_qty = fields.Float(string=u'產品數量', digits=dp.get_precision('Product Unit of Measure'), required=True, index=True)

Monetary

Monetary字段的type值為monetary;column_type值為('numeric', 'numeric')
_slots = {'currency_field': None, 'group_operator': 'sum', }
Monetary 對應pg的字段類型為變長的numeric,並且能執行sum聚合操作
例:
amount = fields.Monetary(string=u'金額', currency_field='company_currency_id')

Char

Char字段的type值為char;column_type值為('varchar', 'VARCHAR')
Char對應pg的字段類型為變長的VARCHAR
例:
name = fields.Char(string=u'名稱', index=True, required=True)

Text

Text字段的type值為text;column_type值為('text', 'text')
Text對應pg的字段類型為長文本類型的text
例:
comment = fields.Text(string=u'評論')

Html

Html字段的type值為text;column_type值為('text', 'text')
Html對應pg的字段類型為長文本類型的text; Html是特殊的Text該字段能保存html代碼
例:
comment = fields.Html(string=u'評論')

Date

Date字段的type值為date;column_type值為('date', 'date')
Date對應pg的字段類型為日期類型的date
例:
start_date = fields.Date(string=u'開始日期', default=fields.Date.today)

Datetime

Datetime字段的type值為datetime;column_type值為('timestamp', 'timestamp')
Datetime對應pg的字段類型為無時區的日期時間類型的timestamp
例:
start_time = fields.Date(string=u'開始時間', default=fields.Date.now)

Binary

Binary字段的type值為binary;column_type值為('bytea', 'bytea')
Binary對應pg的字段類型為二進制類型bytea

Selection

Selection字段的type值為selection;column_type值為('int4', 'integer')或('varchar', 'VARCHAR')
Selection對應pg的字段類型為4個字節的interger或者是不定長的varchar
例:
state = fields.Selection(selection=[('draft',u'草稿'), ('done', u'完成')], default='draft')

Reference

Reference字段的type值為reference;column_type值為('varchar', 'VARCHAR')
Reference對應pg的字段類型為不定長的varchar

Many2one

Many2one字段的type值為many2one;column_type值為('int4', 'int4')
_slots = {'ondelete': 'set null', 'auto_join': False, 'delegate': False, }
Many2one是一個多對一的外表關聯字段,對應pg的字段類型是4個字節的integer
例:
partner_id = fields.Many2one(comodel_name='res.partner', string=u'業務伙伴')

One2many

One2many字段的type值為one2many;
_slots = {'inverse_name': None, 'auto_join': False, 'limit': None,'copy': False,}
One2many是一個一對多的關聯字段,與Manyone形成呼應
例:
stock_quant_ids = fields.One2many(comodel_name='stock.quant', inverse_name='product_id')

Many2many

Many2many字段的type值為many2many;
_slots = {'relation': None, 'column1': None, 'column2': None, 'auto_join': False, 'limit': None,}
many2many是一個多對多的關聯字段
例:
sale_line_ids = fields.Many2many(comodel_name='sale.order.line', relation='sale_order_line_invoice_rel', column1='invoice_line_id', column2='order_line_id', string='Sale Order Lines', readonly=True)



作者:路峰
鏈接:https://www.jianshu.com/p/76d88e2bd5db
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM