【问题标题】:Expected singleton: hr.employee(1, 2)预期单身:hr.employee(1, 2)
【发布时间】:2015-09-28 10:24:11
【问题描述】:

美好的一天!加载kanban 视图时出现错误。我继承了hr.employeeKanban xml,如果某个文档过期,它会在kanban视图中添加过期文档通知,这里是xml代码:

    <record model="ir.ui.view" id="hr_kanban_view_employees_recruitment_kanban">
        <field name="name">HR - Employees Kanban Document Status</field>
        <field name="model">hr.employee</field>
        <field name="inherit_id" ref="hr.hr_kanban_view_employees"/>
        <field name="arch" type="xml">
            <xpath expr="//templates" position="before">
                <field name="employee_id"/>
                <field name="documents_status"/>
            </xpath>
            <xpath expr="//div[@class='oe_employee_details']/ul/li[@id='last_login']" position="inside">
                <span t-if="record.documents_status.raw_value" style="font-size: 100%%"
                        t-att-class="record.documents_status.raw_value==true'oe_kanban_button oe_kanban_color_3'">
                    <field name="employee_id" readonly = "1"/>
                    Has Expired Documents
                </span>
            </xpath>
        </field>
    </record>

以及documents_status 字段的模型

加载时

documents_status = fields.Boolean('DocumentStatus', readonly = True,store = False,compute ='getdocumentStatus')

    @api.one
    def getdocumentStatus(self):
        raise Warning(self.employee_id)
        server_date = datetime.datetime.strptime(DATE_NOW.strftime("%Y-%m-%d") ,"%Y-%m-%d")
        result = {}

        for id in self.ids:
            result[id] = {
                'documents_status': True
            }
            totaldoc = self.env['hr.employee_documents'].search_count([('date_expiry', '<', server_date),('employee_doc_id','=', id)])
            if totaldoc > 0:
                result[id]['documents_status'] = True
                self.documents_status = True
            else:
                result[id]['documents_status'] = False
                self.documents_status = False
        return result

员工中的kanban 视图发生错误

预期的单身人士:hr.employee(1, 2)。

有人能帮我解决这个问题吗?提前致谢。

【问题讨论】:

  • 你可以使用 api.multi 代替 api.one
  • 试过了还是提示错误
  • 为什么要在模型hr.employee中添加一个名为employee_id的字段?是不是和只放id 不一样?

标签: python xml odoo-8 odoo qweb


【解决方案1】:

我不知道hr.employeehr.employee_documents 之间的关系。如果这个是One2many(一个独特员工的许多文档),那么hr.employee 模型中必须有一个One2many 字段指向hr.employee_documents。假设该字段名为documents(这对于通过api.depends 触发计算方法很重要)。然后写这段代码:

@api.multi
@api.depends('documents.date_expiry')
def getdocumentStatus(self):
    server_date = datetime.datetime.strptime(DATE_NOW.strftime("%Y-%m-%d"), "%Y-%m-%d")
    for record in self:
        totaldoc = self.env['hr.employee_documents'].search_count([('date_expiry', '<', server_date),('employee_doc_id','=', self.id)])
        if totaldoc > 0:
            record.documents_status = True
        else:
            record.documents_status = False

你应该把两个模型之间的关系写给我们,给你一个准确的答案。

【讨论】:

  • 感谢代码和答案只是重写我的代码我仍然使用@api.one 但我没有在函数中使用循环来获取特定员工的总过期文档:D 谢谢很多
  • 您好!阅读您的评论,我意识到我的代码有错误,所以我编辑了我的答案。我对self 进行了循环,但我仍在此循环中使用self,而不是record。您可以使用不带循环的@api.one 和带循环的@api.multi。我个人建议您使用@api.multi 和循环:它给我的问题比@api.one 少。顺便说一句,我很高兴我帮助了你。如果您认为正确,请不要忘记将答案设置为正确以关闭问题!
猜你喜欢
  • 2014-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-13
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
  • 2021-11-08
相关资源
最近更新 更多