如何创建一个包含燃料类型和相应默认单价整数值的dict的方法,并使它们以Od中的形式出现

2024-05-15 15:53:08 发布

您现在位置:Python中文网/ 问答频道 /正文

当用户在表单中选择燃料类型时,单价将自动显示所选相应燃料类型的整数值。例如,当用户选择petroleum时,0.87(货币单位为美元)的值应自动显示在单价字段中。你知道吗

下面是我为电台注册创建的类的代码

# Fuel Station register Class
class fleet_fuel_stn_reg(osv.Model):
    _name = 'fleet.fuel.station.reg'
    _description = 'Contains fuel station details to register'

    _columns = {
        'station':fields.char('Station Name'),
        # 'partner':fields.many2one('fleet.partner','Partner', required=True),
        'location':fields.char('Location',  help='Location of the vehicle (garage, ...'),
        'fuel_type': fields.selection([('petroleum', 'Petroleum'), ('diesel', 'Diesel'), ('kerosene', 'Kerosene')], 'Fuel Type', help='Fuel Used by the vehicle'),
        'stocklevel':fields.float('Stock Level'),
        'unit':fields.selection([('litres', 'Litres')], 'Odometer Unit', help='Unit of the odometer ',required=True),
        'unit_price':fields.float('Unit Price'),
        'init_stock':fields.float('Initial Stock'),
        'init_stock_taken':fields.date('Initial Stock Taken In'),
    }

界面如下所示:

enter image description here


Tags: the用户类型fieldsstockhelpunitfloat
1条回答
网友
1楼 · 发布于 2024-05-15 15:53:08

我假设您使用的是Odoo的版本8或更高版本(因为您的问号),所以首先,如果您要创建一个新模型,比如在本例中,您应该使用新的API,这对您来说会容易得多。因此,让我们将您的代码从旧API移到新API,您要查找的内容将通过onchange方法完成:

from openerp import models, fields, api

# Fuel Station register Class
class FleetFuelStnReg(models.Model):
    _name = 'fleet.fuel.station.reg'
    _description = 'Contains fuel station details to register'

    station = fields.Char(
        string='Station Name',
    )
    location = fields.Char(
        string='Location', 
        help='Location of the vehicle (garage, ...)',
    )
    fuel_type = fields.Selection(
        selection=[
            ('petroleum', 'Petroleum'),
            ('diesel', 'Diesel'),
            ('kerosene', 'Kerosene'),
        ],
        string='Fuel Type',
        help='Fuel Used by the vehicle',
    )
    stocklevel = fields.Float(
        string='Stock Level',
    )
    unit = fields.Selection(
        selection=[
            ('litres', 'Litres'),
        ],
        string='Odometer Unit',
        help='Unit of the odometer',
        required=True,
    )
    unit_price: fields.Float(
        string='Unit Price'
    )
    init_stock: fields.Float(
        string='Initial Stock'
    )
    init_stock_taken = fields.Date(
        string='Initial Stock Taken In',
    )

@api.onchange('fuel_type')
def onchange_fuel_type(self):
    if self.fuel_type == 'petroleum':
        self.unit_price = 0.87
    elif self.fuel_type == 'diesel':
        self.unit_price = 0.70
    else:
        self.unit_price = 0.99

编辑

正如您对我所说的燃料类型没有固定的值一样,该字段不能是Selection字段。必须使用Many2one字段,这意味着您必须首先为燃料类型创建一个类:

from openerp import models, fields, api

class FuelType(models.Model):
    _name = 'fuel.type'
    _description = 'All the available fuel types'

    name = fields.Char(
        string='Name',
        required=True,
    )
    price_unit = fields.Float(
        string='Unit price',
        required=True,
    ) 

# Fuel Station register Class
class FleetFuelStnReg(models.Model):
    _name = 'fleet.fuel.station.reg'
    _description = 'Contains fuel station details to register'

    station = fields.Char(
        string='Station Name',
    )
    location = fields.Char(
        string='Location', 
        help='Location of the vehicle (garage, ...)',
    )
    fuel_type = fields.Many2one(
        comodel_name='fuel.type',
        string='Fuel Type',
        help='Fuel Used by the vehicle',
    )
    stocklevel = fields.Float(
        string='Stock Level',
    )
    unit = fields.Selection(
        selection=[
            ('litres', 'Litres'),
        ],
        string='Odometer Unit',
        help='Unit of the odometer',
        required=True,
    )
    unit_price: fields.Float(
        string='Unit Price'
    )
    init_stock: fields.Float(
        string='Initial Stock'
    )
    init_stock_taken = fields.Date(
        string='Initial Stock Taken In',
    )

@api.onchange('fuel_type')
def onchange_fuel_type(self):
    self.unit_price = self.fuel_type.price_unit

相关问题 更多 >