如何通过Odoo中的代码更新特定的员工记录?

时间:2016-08-29 11:24:48

标签: python-2.7 openerp odoo-8

我正在尝试更新员工个人资料(在员工档案中填写一个one2many树列表),如下图所示:

One2many list tree view

但这是基于以下形式,

Assets many2one orm class

现在上面的表格属于&class;" asset.asset'并且有一个很多2 **(分配给)**字段到' hr.employee'类。单击“保存”时,必须在第一张图像的one2many树视图中更新特定资产名称和一些细节。

以下是代码的修改:

asset_asset.py

class asset_asset(osv.osv):
    """
    Assets
    """
    _name = 'asset.asset'
    _description = 'Asset'
    _inherit = ['mail.thread']

    CRITICALITY_SELECTION = [
        ('0', 'General'),
        ('1', 'Important'),
        ('2', 'Very important'),
        ('3', 'Critical')
    ]

    _columns = {
        'name': fields.char('Asset Name', size=64, required=True, translate=True),
        'finance_state_id': fields.many2one('asset.state', 'State', domain=[('team','=','0')]),
        'warehouse_state_id': fields.many2one('asset.state', 'State', domain=[('team','=','1')]),
        'manufacture_state_id': fields.many2one('asset.state', 'State', domain=[('team','=','2')]),
        'maintenance_state_id': fields.many2one('asset.state', 'State', domain=[('team','=','3')]),
        'maintenance_state_color': fields.related('maintenance_state_id', 'state_color', type="selection", selection=STATE_COLOR_SELECTION, string="Color", readonly=True),
        'criticality': fields.selection(CRITICALITY_SELECTION, 'Criticality'),
        'property_stock_asset': fields.property(
          type='many2one',
          relation='stock.location',
          string="Asset Location",
          store=True,
          help="This location will be used as the destination location for installed parts during asset life."),
        'user_id': fields.many2one('hr.employee', 'Assigned to', track_visibility='onchange'),
        'employee_id': fields.many2one('hr.employee', 'Ref'),
        'active': fields.boolean('Active'),
        'profit_id':fields.char('Profit Center', size=64, readonly=True),
        'issue_date':fields.date('Issued On'),
        'return_date':fields.date('Returned On'),
        'test':fields.char('Employee'),
        'asset_number': fields.char('Asset Number', size=64),
        'model': fields.char('Model', size=64),
        'serial': fields.char('Serial no.', size=64),
        'vendor_id':fields.many2one('res.partner', 'Vendor'),
        'manufacturer_id': fields.many2one('res.partner', 'Manufacturer'),
        'start_date': fields.date('Start Date'),
        'purchase_date': fields.date('Purchase Date'),
        'warranty_start_date': fields.date('Warranty Start'),
        'warranty_end_date': fields.date('Warranty End'),
        'maintain_asset': fields.selection([
        ('month', 'Monthly'),
        ('qtr', 'Quarterly'),
        ('half', 'Half Yearly'),
        ('year', 'Yearly'),

        ], 'Maintenance Duration', readonly= False, select=True),
        # 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 asset, limited to 1024x1024px."),
        'image_medium': fields.function(_get_image, fnct_inv=_set_image,
            string="Medium-sized image", type="binary", multi="_get_image",
            store={
                'asset.asset': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
            },
            help="Medium-sized image of the asset. 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={
                'asset.asset': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
            },
            help="Small-sized image of the asset. It is automatically "\
                 "resized as a 64x64px image, with aspect ratio preserved. "\
                 "Use this field anywhere a small image is required."),
    }

    def _employee_get_asset(self, cr, uid,ids, context=None): 

            for rec in self.browse(cr,uid,ids,context=context):
                if user_id:
                    employee = self.pool.get('hr.employee').browse(cr, uid, user_id, context=context)           
                    proj_obj = self.pool.get('hr.employee.asset')
                    values = {'name': rec.name, 'issue_date': rec.issue_date, 'return_date': rec.return_date}
                    proj_obj.write(cr, user, proj_ids, values)
                    proj_obj.create(cr,uid,{'emp_id':rec.user_id, 'name': rec.name, 'issue_date': rec.issue_date, 'return_date':                         rec.return_date},context=context)

            return True

    _defaults = {
        'active': True,
        'employee_id': _employee_get_asset,
    }

员工资料中的one2many树视图的类

class hr_employee_asset(osv.osv):
    _name = "hr.employee.asset"
    _columns = {
            'name': fields.char('Asset Name', size=64),
            'issue_date':fields.date('Issued On'),
            'return_date':fields.date('Returned On'),
            'emp_id':fields.many2one('hr.employee','Employee ID'),

    }

在hr.employee

中添加了one2many字段
class hr_employee(osv.osv):
    _inherit="hr.employee"

    _columns={

        'asset_hr':fields.one2many('asset.asset','employee_id','Employee Assets'),
        'asset_ed':fields.one2many('hr.employee.asset','emp_id','Employee ID'),


    }
hr_employee()

有人请建议我这样做的正确方法,因为我完全搞砸了这个场景。任何指导都非常有价值。

1 个答案:

答案 0 :(得分:0)

感谢您提供有关您情况的详细信息。我认为这个问题与asset.asset中employee_id的默认值声明有关。您引用变量user_id,我没有看到任何值赋值。这应该始终解析False,这意味着

中的代码都没有
if user_id:
    employee = self.pool.get('hr.employee').browse(cr, uid, user_id, context=context)           
    proj_obj = self.pool.get('hr.employee.asset')
    values = {'name': rec.name, 'issue_date': rec.issue_date, 'return_date': rec.return_date}
    proj_obj.write(cr, user, proj_ids, values)
    proj_obj.create(cr,uid,{'emp_id':rec.user_id, 'name': rec.name, 'issue_date': rec.issue_date, 'return_date':                         rec.return_date},context=context)
应该运行

部分。我认为您可能希望完全保留代码中的默认功能。在hr_employee类中,您可以使用asset.asset类定义one2many关系。从员工表单中,如果添加新资产,则应自动分配employee_id,并且不需要进行默认值计算。

您可能需要查看一些可用的日志记录选项。如果您导入日志记录,您应该更好地了解代码中实际发生的情况。无论您的服务器日志存储在何处,都会打印这些日志。您可以将它们放在默认函数中,以便在任何给定时间点查看变量的值,或者只需通过调用_logger.info(“第1节开始”)来标记代码段,以明确函数的每个部分都是按预期运行。

import logging
_logger = logging.getLogger(__name__)

此外,我认为你有一个令人困惑的额外课程。我相信你只需要hr_employee和asset.asset。 hr_employee与asset.asset有一个one2many关系,asset.asset与hr_employee有很多关系。如果您不希望所有字段都显示在hr_employee列表视图中,那么只需在您的xml中选择它们,就像这样。我正在使用asset_ids,因为我认为你应该像在hr_employee中那样定义你的one2many。

<field name="asset_ids">
    <tree>
        <field name="name"/>
        <field name="issue_date"/>
        <field name="return_date"/>
    </tree>
</field>