Reputation: 1
I have a one2many field which contains 3 fields with 2 different values, for example. Here let's say the Zone is a one2many field
Zone A = Car = 3000, Bike = 2000.
Zone B = Car = 2500, Bike = 1500.
Zone C = Car = 2000, Bike = 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)
the point is I want to select the "A" zone, then I select "Car" in many2one fields
the output at the rate field is 3000,
and if I select zone "B" then select "Bike" the output at the rate field is 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)
In Open ERP domain filter
['|',('a','=',5),('&',('b','!=',10),('c','=',12))]
https://stackoverflow.com/a/19070664/9228786
Thank you in advance
Upvotes: 0
Views: 186
Reputation: 2135
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.
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
Upvotes: 0