This question has been flagged
1 Reply
7998 Views

Hiiii...

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.

Query;

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!! :D

Avatar
Discard
Best Answer

Is Your structure like this:

ZOne Table

Zone
A
B
C

Vehicle Table

Vehicle Name
Car
Bike


Rates Table

Zone ID(m2o)Vehiche Type ID (M2o)Rates(monetary/float)
A
Car
3000
A
Bike
2000
B
Car
2500
B
Bike
1500
C
Car
2000
C
Bike
1000

(Constrains between vehicle and zone), no same vehicle on same zone,



Your Transaction Table

zone_id (M2o)Vehicle ID (M2o)Rates(monetary/float)
A
Car
3000                
A
Bike
1500


If the structure that i wrote was right, then you just only set a onchange action on vehicle_id and zone_id in your transaction

@api.onchange('zone_id','vehicle_id')
def onchange_get_rates(self):
    if self.zone_id.id and self.vehicle_id.id:
        # calculate here
        # find in rates table
        rates = self.env['rates.table'].search([('zone_id','=',zone), ('vehicle_type_id','=',vehicle_type_id)])
        self.rates = rates.rates


But..! if "rates table" doesn't had any constrains between vehicle and zone,,
i mean than maybe in same zone has many rates for 1 same vehicle (example: [id:1] Zone A - Car -Rates = 3000, Zone A, [id:2] Zone A - Car - Rates = 3500)
so let the user select the rates based on zone and vehicle type, you must use onchange too,, where change modify fields domain, but in this case rates field was many2one

Ex:

Your Transaction Table

zone_id (M2o)Vehicle ID (M2o)Rate ID (m2o)
A
Car
1
A
Bike
2



so return domain change in onchange method

return {
    'domain':{
        'field_name':[(domain,'=',value)]
    }
}

Avatar
Discard
Author

I will try sir..

Big Thanks for response (Terimakasih banyak) :D

Author

i get error code ;

File "/home/fazryan/odoo/odoo/modules/registry.py", line 170, in __getitem__

return self.models[model_name]

KeyError: 'rates.table'

and this code that I implemented;

@api.onchange('parking_zone_rate_id','vehicle_typee_id')

def onchange_get_rates(self):

if self.parking_zone_rate_id and self.vehicle_typee_id:

# calculate here

# find in rates table

rates = self.env['rates.table'].search([('parking_zone_rate_id','=','name'), ('name','=','name')])

self.rates = rates.rates

please guide me, what should I do?

Usually in python, if that error showed up it caused by you try to call an attribute that not defined in object

And as you need to know that, not all that syntax that i wrote before was correct because i never test the code,, it just only to show you to try the flow/concept,,

It very depends on your model structure

So please understand the concept/sructure and flow,,, not to copy the code that i wrote before

I suggest you to explain your model structure more detail..

In that error message that class model "'rates.table" doesn't not defined...

Regards,