简体   繁体   English

将字段域从一对多过滤到一对一

[英]Filter field domain one2many to many2one

I have a one2many field which contains 3 fields with 2 different values, for example. 我有一个one2many字段,其中包含3个带有2个不同值的字段。 Here let's say the Zone is a one2many field 假设区域是一个one2many字段

Zone A = Car = 3000, Bike = 2000. A区=汽车= 3000,自行车= 2000。

Zone B = Car = 2500, Bike = 1500. B区=汽车= 2500,自行车= 1500。

Zone C = Car = 2000, Bike = 1000. C区=汽车= 2000,自行车= 1000。

and I have many2one fields for the chosen field later (Ex. Car and Bike) 我稍后会为所选字段提供许多字段(例如,汽车和自行车)

and rate_fields as trigger fields for calculations(a place to store value later) 和rate_fields作为计算的触发字段(以后存储值的地方)

the point is I want to select the "A" zone, then I select "Car" in many2one fields 关键是我要选择“ A”区域,然后在many2one字段中选择“ Car”

the output at the rate field is 3000, 费率字段的输出为3000,

and if I select zone "B" then select "Bike" the output at the rate field is 1500 如果我选择区域“ B”,然后选择“自行车”,则费率字段的输出为1500

if written with code, then the implementation uses filter by domain with domain syntax Multiple Conditions. 如果用代码编写,则实现将使用具有域语法“多个条件”的按域过滤。 can anyone help me to make an example code? 谁能帮我做一个示例代码?

maybe this is a reference but I can't make the appropriate code 也许这是一个参考,但我无法制作适当的代码

Multiple Conditions 多种条件

In Programming 在编程中

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

In Open ERP domain filter 在Open ERP域过滤器中

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

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

Thank you in advance 先感谢您

I referenced your other question for some more details, but your questions are both very confusing. 我参考了您的其他问题以获取更多详细信息,但是您的问题都很混乱。 As far as I understand, your goal is to choose a Zone, then choose a Vehicle Type. 据我了解,您的目标是选择一个区域,然后选择一种车辆类型。 Based on your choices, you want to see the Rate. 根据您的选择,您想要查看费率。

Whatever model you want to have the calculation will need a field to choose the Zone, a field to choose the Vehicle Type, and a field to store the Rate. 无论您要进行计算的哪种模型,都需要一个字段来选择区域,一个字段来选择车辆类型以及一个字段来存储费率。

Your other question's classes are a bit messy, so here's what I suggest. 您其他问题的课程有点混乱,所以这是我的建议。

You'll need a model to track (1) Locations/Zones, (2) Vehicle Types, (3) Rate each Vehicle Type is charged at each Location/Zone, and (4) a model to compute the Rate for the given Vehicle Type at the given Location/Zone. 您需要一个模型来跟踪(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM