首页 > 解决方案 > 过滤字段域 one2many to many2one

问题描述

例如,我有一个 one2many 字段,其中包含 3 个具有 2 个不同值的字段。这里假设 Zone 是 one2many 字段

A 区 = 汽车 = 3000,自行车 = 2000。

B 区 = 汽车 = 2500,自行车 = 1500。

C 区 = 汽车 = 2000,自行车 = 1000。

稍后我有许多用于所选字段的字段(例如汽车和自行车)

和 rate_fields 作为计算的触发字段(稍后存储值的地方)

关键是我想选择“A”区域,然后我在 many2one 字段中选择“汽车”

rate 字段的输出为 3000,

如果我选择区域“B”然后选择“自行车”,则速率字段的输出为 1500

如果用代码编写,则实现使用域语法多个条件按域过滤。谁能帮我做一个示例代码?

也许这是一个参考,但我无法制作适当的代码

多个条件

在编程中

如果 a = 5 或 (b != 10 and c = 12)

在打开 ERP 域过滤器中

['|',('a','=',5),('&',('b','!=',10),('c','=',12))]

https://stackoverflow.com/a/19070664/9228786

先感谢您

标签: pythonpython-2.7odooodoo-10

解决方案


我参考了您的其他问题以获取更多详细信息,但是您的问题都非常令人困惑。据我了解,您的目标是选择一个区域,然后选择一种车辆类型。根据您的选择,您希望查看费率。

无论您想要计算哪种模型,都需要一个字段来选择区域,一个字段来选择车辆类型,以及一个字段来存储费率。

你的其他问题的课程有点乱,所以这就是我的建议。

您需要一个模型来跟踪 (1) 位置/区域,(2) 车辆类型,(3) 在每个位置/区域对每种车辆类型收费,以及 (4) 一个模型来计算给定车辆的费率在给定的位置/区域键入。

class ParkingLocation(models.Model):
    _name = 'parking.location'
    _description = 'Parking Zones'

    name = fields.Char(string='Name')

class VehicleType(models.Model):
    _name = 'vehicle.type'
    _description = 'Types of Vehicles'

    name = fields.Char(string='Name')

class ZoneRate(models.Model):
    _name = 'zone.rate'
    _description = 'Define the rate for each Vehicle Type in each Zone'

    location_id = fields.Many2one('parking.location', string='Location', required='True')
    vehicle_type_id = fields.Many2one('vehicle.type', string='Vehicle Type')
    rate = fields.Float('Rate')

class ZoneRateLookup(models.Model):
    _name = 'zone.rate.lookup'
    _description = 'Calculate the rate for the chosen Zone and Vehicle Type'

    location_id = fields.Many2one('parking.location', string='Location', required='True')
    vehicle_type_id = fields.Many2one('vehicle.type', string='Vehicle Type')
    rate = fields.Float('Rate', compute='_compute_rate', store=True, readonly=True)

    @api.multi
    @api.depends('location_id', 'vehicle_type_id')
    def _compute_rate(self):
        rate_lookup_obj = self.env['zone.rate.lookup']
        for zone_rate in self:
            rate = rate_lookup_obj.search([('location_id', '=', zone_rate.location_id.id),
                                                     ('vehicle_type_id', '=', zone_rate.vehicle_type_id.id)])
            if not rate:
                raise ValidationError(_('No Rate found for that Vehicle Type in that Zone!')
            zone_rate.rate = rate.rate

推荐阅读