【问题标题】:Odoo. One to many search by field奥多。按字段一对多搜索
【发布时间】:2015-11-13 17:07:36
【问题描述】:

我在 odoo 9 中为员工扩展模块。每个员工都有孩子。这是我的模型:

class Employee(models.Model):
    _name = 'hr.employee'
    _inherit = 'hr.employee'

    children_ids = fields.One2many('my_module', 'employee_id', string="Children")


class Child(models.Model):
    _name = 'my_module'

    name = fields.Char(required=True, string='Name')

    birthday = fields.Date(required=True, string='Birthday')

    employee_id = fields.Many2one('hr.employee', ondelete='cascade', string="Employee")

如何配置按生日搜索?例如,我想在搜索中设置生日日期(或日期间隔),系统必须从搜索中显示所有有生日孩子的员工。

这是我的搜索视图(按孩子的名字工作):

<record id="view_urspectr_employee_filter" model="ir.ui.view">
    <field name="name">Employees</field>
    <field name="model">hr.employee</field>
    <field name="inherit_id" ref="hr.view_employee_filter" />
    <field name="arch" type="xml">
        <search position="inside">
            <field name="children_ids"/>
        </search>
    </field>
</record>

我在模块account(../addons/account/views/account_view.xml)中发现了类似的情况:

<record id="view_account_move_filter" model="ir.ui.view">
            <field name="name">account.move.select</field>
            <field name="model">account.move</field>
            <field name="arch" type="xml">
                <search string="Search Move">
                    <field name="name" filter_domain="['|', ('name','ilike',self), ('ref','ilike',self)]" string="Move"/>
                    <field name="date"/>

按日期搜索很好。但在这种情况下,date 字段使用没有关系 One2Many

你能帮忙解决吗?我找不到通过 One2Many 搜索的示例。 提前谢谢你。

【问题讨论】:

    标签: python openerp odoo-9


    【解决方案1】:

    这里的解决方案:

    <record id="view_urspectr_employee_filter" model="ir.ui.view">
        <field name="name">Employees</field>
        <field name="model">hr.employee</field>
        <field name="inherit_id" ref="hr.view_employee_filter"/>
        <field name="arch" type="xml">
            <search position="inside">
                <field name="children_ids" filter_domain="[('children_ids.birthday', '=', self)]" widget="date"/>
            </search>
        </field>
    </record>
    

    【讨论】:

    • 为什么 OP 应该尝试这个?一个好的答案总是会解释所做的事情以及这样做的原因,不仅适用于 OP,而且适用于 SO 的未来访问者。
    • @JayBlanchard 我认为使用 odoo 的访问者很清楚。他们可以使用此解决方案通过 one2many 关系按日期进行搜索。
    • 对于新手来说可能不太清楚。你必须考虑观众。还有一点,您一直在编辑问题中的代码,有时还改变了 OP 发布的内容的性质。 不要那样做。
    • 谢谢。示例:&lt;field name="children_ids" filter_domain="[('children_ids.birthday, '=', self)]" /&gt; 帮助我按字段过滤了我的 many2one 字段。
    • @IstaLibera 我很高兴能提供帮助。祝你好运;)
    【解决方案2】:

    在这里,您可以尝试在“hr.employee”表中添加与“children_ids”相关的“birthday”相关字段,但问题仍然可能是它可能无法仅搜索“2015” ' 因为“生日”字段的数据类型是“日期”,而不仅仅是简单的字符串。不过你可以试一试。

    【讨论】:

    • 我知道。但是我如何配置按日期搜索(如果日期在另一个模型中)?
    • 我还没有看到在另一个模型中按字段搜索的示例。
    • 如果您在“hr.employee”中填写相关字段,您可以输入完整日期...例如 2015-01-01。
    • 好的。我更新了问题。您能否提供示例如何按日期添加过滤器?现在它只适用于孩子的名字。
    • 尝试查看'sale.order'和'sale.order.line'模型的示例,其中sales.order.line模型的字段product_id属于sale.order模型作为相关字段并且用于 sale.order 模型的搜索视图。尝试搜索视图 ID 'view_sales_order_filter' 或视图名称 'sale.order.list.select'。所以在这里您正在搜索 one2many 模型中的产品。在您的情况下,您需要将“生日”字段作为“hr.employee”模型中的相关字段。
    猜你喜欢
    • 2020-10-08
    • 1970-01-01
    • 2019-01-26
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多