填充上改变odoo 9
问题描述:
选择选项如何填充其它变更字段中选择选项。例如:填充上改变odoo 9
用于选择选项默认值为存储在数据库tbl_car(奥迪,欧宝,奔驰,VW,BMW)。在其他表tbl_car_user中,我存储car_name和user_name('Peter','Audi')。现在我想要更改后的用户ID(选择用户彼得)在汽车选择选项获取所有汽车不包括奥迪(用户彼得已经使用奥迪)。
也许是这样的:
for car in self.env['tbl.car'].search([]):
for car_user in self.env['car.user'].search([('user_id','=','self.user_id.id]):
if (car.name = car_user.name):
print("DUPLICATE")
else:
print("ADD TO SELECT OPTION")
任何简单的解决方案?
答
我的第一answar是正确的,现在我会给出一个解决方案,如果你不想改变选择:
创建向导来影响汽车用户:
class AffectCar(model.TransientModel):
_name = 'affect.user.car.wizard'
use_id = fields.Many2one(..) # you know how you do it
car_name = fields.Selection(selection='_get_car_selection', 'Car name')
def _get_car_selection(self):
"""
generate a selection for field car_name according to
the default user_id passed to this form
"""
# get all car name that this user don't have
# generate the selection [('car_name','car_name')..]
return computed_selection
def create_user_car(self):
""" save a new tbbl_car_user record """
# this method is called from the form of the wizard
# save the user_id and the car_name in tbl_car_user
现在加入钮给用户的形式和调用一个方法来与默认USER_ID打开向导形式是 同一用户
@api.multi()
def add_car(self):
"""
open the wizard to add a car
to this user
"""
return {
'type': 'ir.actions.act_window',
'view_mode': 'form',
'view_type': 'form',
'res_model':'affect.user.car.wizard',
'target': 'new',
'context': {
# pass the id the the user to the wizard
'default_use_id': self.id,
},
}
一件事情,以防止你的用户APPLICATIO n显示弹出窗口时更改user_id 使用户在向导的窗体视图中隐藏=“1”
<record id="add_car_wizard" model="ir.ui.view">
<field name="name">tax.adjustments.wizard.form</field>
<field name="model">tax.adjustments.wizard</field>
<field name="arch" type="xml">
<form>
<group>
<field name="user_id" invisible="1"/>
<field name="car_name"/>
</group>
<footer>
<button name="create_user_car" string="Add car" type="object" class="oe_highlight"/>
or
<button string="Cancel" special="cancel" />
</footer>
</form>
</field>
</record>
答
这样的问题不使用的选择,甚至当你发现这一点,如果编辑记录下一次选择不会知道它包含的价值,因为odoo将被除的值的所有值填充选区是它有。你会在选择领域看到未知的价值。
,但如果你想这样做不使用选择使用many2one车名的选择更改为一个模型(表数据库),并使用域名为您many2one领域。
你不能做到通过选择这个逻辑这个逻辑可以不要与选择只为向导。
field_selection = fields.Selection(selection='generate_selection')
def generate_selection(self):
# compute selection
return computed_selection
但是当视图是加载在第一时间现在选择的值不能被编辑或onchange事件改变其工作原理。