使用 python 在 odoo 10 中创建从 header 到详细信息的域
create domain from header to detail in odoo 10 using python
我不知道我的代码有什么问题。我有两个模型,一个是 payroll.adjustment 我在 xml 中使用的模型,一个是 payroll.adjustment.lines。我需要做的就是在 payroll.adjustment.lines 上的字段中创建一个域,即 employee_id,这就是我所做的。
<field name="employee_id" options="{'no_create': True}" domain="
[('address_id','=',company_id)]"/>
但是显示错误"Uncaught Error: NameError: name 'company_id' is not defined"
我在 .py 中的代码
from odoo import models, fields, api
from odoo.exceptions import UserError, ValidationError
class PayrollAdjustment(models.Model):
_name = 'payroll.adjustment'
_description ='Payroll Adjustment'
name = fields.Char(string="Name", related='doc_num')
doc_num = fields.Char(string="Document No.")
state = fields.Selection([('draft', 'Draft'),
('confirm', 'Waiting for Approval'),
('approved','Approved'),
('void', 'Void')],default='draft')
company_id = fields.Many2one('res.partner',string="Company",domain="
[('is_company','=',True)]")
date_from = fields.Date(string="Date From", required= True)
date_to = fields.Date(string="Date To", required= True)
adjustment_lines =
fields.One2many('payroll.adjustment.lines','adj_id',string="Adjustment
lines")
color = fields.Integer()
sample = fields.Many2one ('hr.employee')
@api.multi
def action_draft(self):
self.state = 'draft'
@api.multi
def action_confirm(self):
self.state = 'confirm'
@api.multi
def action_approve(self):
self.state = 'approved'
@api.multi
def action_void(self):
self.state = 'void'
@api.model
def create(self, vals):
vals['name'] = self.env['ir.sequence'].get('payroll.adj.seq')
return super(PayrollAdjustment, self).create(vals)
class PayrollAdjustmentLines(models.Model):
_name = 'payroll.adjustment.lines'
_description = 'Payroll Adjustment Lines'
_inherit = ['mail.thread']
employee_id = fields.Many2one('hr.employee',
string="Employee",required=True)
adjustment_for = fields.Many2one('hr.salary.rule', string="Adjustment
for",domain=[('appears_on_adj', '=', True)], required=True)
remarks = fields.Char(string="Remarks")
amount = fields.Float(string="Amount", default = 0.00)
adj_id = fields.Many2one('payroll.adjustment',string="Payroll
Adjustment",ondelete='cascade')
我的代码。xml
<record id="payroll_adjustment_form_view" model="ir.ui.view">
<field name="name">payroll_adjustment.form</field>
<field name="model">payroll.adjustment</field>
<field name="arch" type="xml">
<form>
<header>
<button name="action_draft" type="object" string="Set
back to draft" states="confirm"/>
<button name="action_confirm" type="object"
string="Confirm" states="draft" class="oe_highlight"/>
<button name="action_approve" type="object"
string="Approve" states="confirm" class="oe_highlight"
groups="hr_payroll.group_hr_payroll_manager"/>
<button name="action_void" type="object" string="Void"
states="confirm,approved" class="oe_highlight"
groups="hr_payroll.group_hr_payroll_manager"/>
<field name="state" widget="statusbar"/>
</header>
<sheet>
<div class="pull-left">
<label for="doc_num" class="oe_edit_only oe_inline"/>
<h1>
<field name="doc_num" readonly="1"/>
</h1>
</div>
<group>
<label for="company_id"/>
<div>
<field name="company_id" class="oe_inline"
attrs="{'readonly': [('state','not in',('draft'))]}"/>
</div>
<label for="date_from" string="Period"/>
<div>
<field name="date_from" class="oe_inline"
attrs="{'readonly': [('state','not in',('draft'))]}"/> -
<field name="date_to" class="oe_inline" attrs="
{'readonly': [('state','not in',('draft'))]}"/>
<field name="sample" domain="
[('address_id','=',company_id)]"/>
</div>
</group>
<field name="adjustment_lines" attrs="{'readonly':
[('state','not in',('draft'))]}" context="{'company': company_id }">
<tree editable="1">
<field name="employee_id" options="{'no_create':
True}" domain="[('address_id','=',company_id)]"/>
<field name="adjustment_for" options="
{'no_create': True}"/>
<field name="amount"/>
<field name="remarks"/>
</tree>
</field>
</sheet>
</form>
</field>
</record>
请帮助我。提前致谢
您正在 'payroll.adjustment.lines' 模型的字段 employee_id 中添加域,但 'company_id' 字段不在该模型中,但它在父模型 'payroll.adjustment' 中。
所以你应该这样写:
<field name="employee_id" options="{'no_create': True}" domain="
[('address_id','=',parent.company_id)]"/>
我不知道我的代码有什么问题。我有两个模型,一个是 payroll.adjustment 我在 xml 中使用的模型,一个是 payroll.adjustment.lines。我需要做的就是在 payroll.adjustment.lines 上的字段中创建一个域,即 employee_id,这就是我所做的。
<field name="employee_id" options="{'no_create': True}" domain="
[('address_id','=',company_id)]"/>
但是显示错误"Uncaught Error: NameError: name 'company_id' is not defined"
我在 .py 中的代码
from odoo import models, fields, api
from odoo.exceptions import UserError, ValidationError
class PayrollAdjustment(models.Model):
_name = 'payroll.adjustment'
_description ='Payroll Adjustment'
name = fields.Char(string="Name", related='doc_num')
doc_num = fields.Char(string="Document No.")
state = fields.Selection([('draft', 'Draft'),
('confirm', 'Waiting for Approval'),
('approved','Approved'),
('void', 'Void')],default='draft')
company_id = fields.Many2one('res.partner',string="Company",domain="
[('is_company','=',True)]")
date_from = fields.Date(string="Date From", required= True)
date_to = fields.Date(string="Date To", required= True)
adjustment_lines =
fields.One2many('payroll.adjustment.lines','adj_id',string="Adjustment
lines")
color = fields.Integer()
sample = fields.Many2one ('hr.employee')
@api.multi
def action_draft(self):
self.state = 'draft'
@api.multi
def action_confirm(self):
self.state = 'confirm'
@api.multi
def action_approve(self):
self.state = 'approved'
@api.multi
def action_void(self):
self.state = 'void'
@api.model
def create(self, vals):
vals['name'] = self.env['ir.sequence'].get('payroll.adj.seq')
return super(PayrollAdjustment, self).create(vals)
class PayrollAdjustmentLines(models.Model):
_name = 'payroll.adjustment.lines'
_description = 'Payroll Adjustment Lines'
_inherit = ['mail.thread']
employee_id = fields.Many2one('hr.employee',
string="Employee",required=True)
adjustment_for = fields.Many2one('hr.salary.rule', string="Adjustment
for",domain=[('appears_on_adj', '=', True)], required=True)
remarks = fields.Char(string="Remarks")
amount = fields.Float(string="Amount", default = 0.00)
adj_id = fields.Many2one('payroll.adjustment',string="Payroll
Adjustment",ondelete='cascade')
我的代码。xml
<record id="payroll_adjustment_form_view" model="ir.ui.view">
<field name="name">payroll_adjustment.form</field>
<field name="model">payroll.adjustment</field>
<field name="arch" type="xml">
<form>
<header>
<button name="action_draft" type="object" string="Set
back to draft" states="confirm"/>
<button name="action_confirm" type="object"
string="Confirm" states="draft" class="oe_highlight"/>
<button name="action_approve" type="object"
string="Approve" states="confirm" class="oe_highlight"
groups="hr_payroll.group_hr_payroll_manager"/>
<button name="action_void" type="object" string="Void"
states="confirm,approved" class="oe_highlight"
groups="hr_payroll.group_hr_payroll_manager"/>
<field name="state" widget="statusbar"/>
</header>
<sheet>
<div class="pull-left">
<label for="doc_num" class="oe_edit_only oe_inline"/>
<h1>
<field name="doc_num" readonly="1"/>
</h1>
</div>
<group>
<label for="company_id"/>
<div>
<field name="company_id" class="oe_inline"
attrs="{'readonly': [('state','not in',('draft'))]}"/>
</div>
<label for="date_from" string="Period"/>
<div>
<field name="date_from" class="oe_inline"
attrs="{'readonly': [('state','not in',('draft'))]}"/> -
<field name="date_to" class="oe_inline" attrs="
{'readonly': [('state','not in',('draft'))]}"/>
<field name="sample" domain="
[('address_id','=',company_id)]"/>
</div>
</group>
<field name="adjustment_lines" attrs="{'readonly':
[('state','not in',('draft'))]}" context="{'company': company_id }">
<tree editable="1">
<field name="employee_id" options="{'no_create':
True}" domain="[('address_id','=',company_id)]"/>
<field name="adjustment_for" options="
{'no_create': True}"/>
<field name="amount"/>
<field name="remarks"/>
</tree>
</field>
</sheet>
</form>
</field>
</record>
请帮助我。提前致谢
您正在 'payroll.adjustment.lines' 模型的字段 employee_id 中添加域,但 'company_id' 字段不在该模型中,但它在父模型 'payroll.adjustment' 中。
所以你应该这样写:
<field name="employee_id" options="{'no_create': True}" domain="
[('address_id','=',parent.company_id)]"/>