python - 过滤字段域 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
先感谢您
解决方案
我参考了您的其他问题以获取更多详细信息,但是您的问题都非常令人困惑。据我了解,您的目标是选择一个区域,然后选择一种车辆类型。根据您的选择,您希望查看费率。
无论您想要计算哪种模型,都需要一个字段来选择区域,一个字段来选择车辆类型,以及一个字段来存储费率。
你的其他问题的课程有点乱,所以这就是我的建议。
您需要一个模型来跟踪 (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
推荐阅读
- python - 模块“tensorflow.tools.docs.doc_controls”没有属性“inheritable_header”
- webrtc - 如何从外部耳机检测物理静音/取消静音麦克风开/关按钮?
- gitlab - 与 gitlab 集成时无法访问 SonarQube 服务器
- macos - 由于白色突出显示,Mac 上的 Lyx 数学无法读取
- python - 如何通过检查 2 列的交集来组合集合的数据框
- vue.js - 如何使库在 Composition API 中全局可用?
- javascript - 为什么我在同时执行多个请求时收到此队列并停滞时间
- amazon-cloudformation - ECS TaskDefinition 创建失败,并显示“提供的请求无效:创建 TaskDefinition:未知卷 'null'”。
- r - 在表中显示事后 Tukey 分析的问题
- fortran - fortran 代码适用于真正的 4,但不适用于真正的 16