openerp学习笔记 对象继承,对象初始化数据
1.对象继承
_inherit = "product.product"
继承产品对象,给产品对象添加字段或方法,不需要设置 _name、_table 等属性
注意:当继承后的子类不定义 _name 属性,则相当于在父类中增加字段和方法,并不创建新对象
当继承后的子类重新定义 _name
属性,则创建一个新的对象,新对象拥有父类中所有的字段和方法,父类不受任何影响。
_inherits = {'product.template':
'product_tmpl_id'}
继承产品模版对象,创建新的产品对象,产品对象与产品对象之间建立多对一关联关系,产品模版中的字段可以等同产品中的字段一样使用
注意:相当于多重集成。子类通过 _inherits
中定义的字段和各个父类关联,子类不拥有父类的字段,但可以直接操作父类的所有字段和方法。
_inherit = ["calendar.event", "mail.thread", "ir.needaction_mixin"]
多重继承,一般用于继承抽象类,调用类中的方法,不创建字段?。如 mail.thread 为邮件对象,用于发送消息。
2.对象初始化数据
def init(self, cr):
安装对象所属模块时初始化对象数据
示例代码:
class product_product(osv.osv):
_inherit =
"product.product"
def init(self,
cr):
#For the first installation
if you already have product in your database the name of the existing product
will be empty, so we fill it
cr.execute("update product_product set name=name_template where name is
null;")
return True
_columns = {
'name': fields.char('Name', size=128, translate=True,
select=True),
'variants':
fields.char('Variants', size=128),
'dimension_value_ids': fields.many2many('product.variant.dimension.value',
'product_product_dimension_rel', 'product_id', 'dimension_id', 'Dimensions',
domain="[('product_tmpl_id','=',product_tmpl_id)]"),
'cost_price_extra' : fields.float('Purchase Extra Cost',
digits_compute=dp.get_precision('Purchase
Price')),
'lst_price' :
fields.function(_product_lst_price, method=True, type='float', string='List
Price', digits_compute=dp.get_precision('Sale
Price')),
#the way the weight are
implemented are not clean at all, we should redesign the module product form the
addons in order to get something
correclty.
#indeed some field of
the template have to be overwrited like weight, name, weight_net,
volume.
#in order to have a
consitent api we should use the same field for getting the weight, now we have
to use "weight" or "total_weight" not clean at all with external
syncronization
'total_weight':
fields.function(_product_compute_weight_volume, method=True, type='float',
string='Gross weight', help="The gross weight in Kg.",
multi='weight_volume'),
'total_weight_net': fields.function(_product_compute_weight_volume, method=True,
type='float', string='Net weight', help="The net weight in Kg.",
multi='weight_volume'),
'total_volume': fields.function(_product_compute_weight_volume,
method=True, type='float', string='Volume', help="The volume in m3.",
multi='weight_volume'),
'additional_weight': fields.float('Additional Gross weight', help="The
additional gross weight in Kg."),
'additional_weight_net': fields.float('Additional Net weight', help="The
additional net weight in Kg."),
'additional_volume': fields.float('Additional Volume', help="The additional
volume in Kg."),
}
class product_product(osv.osv):
_name = "product.product"
_description = "Product"
_table =
"product_product"
_inherits = {'product.template':
'product_tmpl_id'}
_inherit =
['mail.thread']
_order =
'default_code,name_template'
_columns =
{
'qty_available':
fields.function(_product_qty_available, type='float', string='Quantity On
Hand'),
'virtual_available':
fields.function(_product_virtual_available, type='float', string='Quantity
Available'),
'incoming_qty':
fields.function(_product_incoming_qty, type='float',
string='Incoming'),
'outgoing_qty': fields.function(_product_outgoing_qty, type='float',
string='Outgoing'),
'price':
fields.function(_product_price, type='float', string='Price',
digits_compute=dp.get_precision('Product
Price')),
'lst_price' :
fields.function(_product_lst_price, type='float', string='Public Price',
digits_compute=dp.get_precision('Product
Price')),
'code':
fields.function(_product_code, type='char', string='Internal
Reference'),
'partner_ref' :
fields.function(_product_partner_ref, type='char', string='Customer
ref'),
'default_code' :
fields.char('Internal Reference', size=64,
select=True),
'active':
fields.boolean('Active', help="If unchecked, it will allow you to hide the
product without removing it."),
'variants': fields.char('Variants',
size=64),
'product_tmpl_id': fields.many2one('product.template', 'Product
Template', required=True, ondelete="cascade",
select=True),
'ean13':
fields.char('EAN13 Barcode', size=13, help="International Article Number used
for product identification."),
'packaging' : fields.one2many('product.packaging', 'product_id', 'Logistical
Units', help="Gives the different ways to package the same product. This has no
impact on the picking order and is mainly used if you use the EDI
module."),
'price_extra':
fields.float('Variant Price Extra', digits_compute=dp.get_precision('Product
Price')),
'price_margin':
fields.float('Variant Price Margin', digits_compute=dp.get_precision('Product
Price')),
'pricelist_id':
fields.dummy(string='Pricelist', relation='product.pricelist',
type='many2one'),
'name_template':
fields.related('product_tmpl_id', 'name', string="Template Name", type='char',
size=128, store=True,
select=True),
'color':
fields.integer('Color Index'),
#
image: all image fields are base64 encoded and
PIL-supported
'image':
fields.binary("Image",
help="This field holds the image used as image for the product, limited to
1024x1024px."),
'image_medium':
fields.function(_get_image,
fnct_inv=_set_image,
string="Medium-sized image", type="binary",
multi="_get_image",
store={
'product.product': (lambda self, cr, uid, ids, c={}: ids, ['image'],
10),
},
help="Medium-sized image of the product. It is automatically
"\
"resized as a 128x128px image, with aspect ratio preserved,
"\
"only when the image exceeds one of those sizes. Use this field in form views or
some kanban views."),
'image_small': fields.function(_get_image,
fnct_inv=_set_image,
string="Small-sized image", type="binary",
multi="_get_image",
store={
'product.product': (lambda self, cr, uid, ids, c={}: ids, ['image'],
10),
},
help="Small-sized image of the product. It is automatically
"\
"resized as a 64x64px image, with aspect ratio preserved.
"\
"Use this field anywhere a small image is
required."),
'seller_info_id':
fields.function(_calc_seller, type='many2one', relation="product.supplierinfo",
string="Supplier Info",
multi="seller_info"),
'seller_delay': fields.function(_calc_seller, type='integer', string='Supplier
Lead Time', multi="seller_info", help="This is the average delay in days between
the purchase order confirmation and the reception of goods for this product and
for the default supplier. It is used by the scheduler to order requests based on
reordering delays."),
'seller_qty': fields.function(_calc_seller, type='float', string='Supplier
Quantity', multi="seller_info", help="This is minimum quantity to purchase from
Main Supplier."),
'seller_id':
fields.function(_calc_seller, type='many2one', relation="res.partner",
string='Main Supplier', help="Main Supplier who has highest priority in Supplier
List.", multi="seller_info"),
}
class crm_meeting(base_state,
osv.Model):
""" Model for CRM meetings
"""
_name = 'crm.meeting'
_description = "Meeting"
_order = "id
desc"
_inherit = ["calendar.event", "mail.thread",
"ir.needaction_mixin"]
_columns =
{
# base_state required
fields
'create_date':
fields.datetime('Creation Date',
readonly=True),
'write_date':
fields.datetime('Write Date',
readonly=True),
'date_open':
fields.datetime('Confirmed',
readonly=True),
'date_closed':
fields.datetime('Closed',
readonly=True),
'partner_ids':
fields.many2many('res.partner', 'crm_meeting_partner_rel', 'meeting_id',
'partner_id',
string='Attendees', states={'done': [('readonly',
True)]}),
'state':
fields.selection(
[('draft', 'Unconfirmed'), ('open',
'Confirmed')],
string='Status', size=16, readonly=True,
track_visibility='onchange'),
#
Meeting fields
'name':
fields.char('Meeting Subject', size=128, required=True, states={'done':
[('readonly', True)]}),
'categ_ids': fields.many2many('crm.meeting.type',
'meeting_category_rel',
'event_id', 'type_id', 'Tags'),
'attendee_ids': fields.many2many('calendar.attendee',
'meeting_attendee_rel',\
'event_id', 'attendee_id', 'Attendees', states={'done': [('readonly',
True)]}),
}