跳至内容
菜单
此问题已终结
3 回复
6319 查看

Hello odooers,

I hope you a are doing well.

I have one question. I want to use many2one field as dynamic e.g. dynamic model name on onchange of some field.


Actually i have taken one many2one field model_id (ir.model) on onchange of model_id i want to set that model as many2one field relation with field.


I have tried with many ways, didn't find any solution. onchange return attribute also not works like dynamic domain.


Thanks in advance. 


Regards

Haresh Kansara

形象
丢弃
最佳答案

There a field called Reference
Example of how to use it:

fields.Reference(selection='_ select_target_model', string="Source Document")


形象
丢弃
最佳答案

Reference field is perfectly fit with your requirement. also, it doesn't require to take many2one field in ir.model
https://github.com/odoo/odoo/blob/12.0/odoo/addons/test_new_api/models.py#L274


形象
丢弃
编写者

thanks, it's very helpful for me. But now i have question, in reference field i can select only one record as many2one. So is there any way to select multiple records from reference field?

Thanks

No there is no way to select multiple records.

but if you want to achieve by extending reference field many2one part with many2many at client and server side

最佳答案

Hi,

Actually if you achieved it some how means, How it will work ? It will generate inconsistency of the data.  Suppose if in the first record, you choose the model as res.partner , so right now the co-model of dynamic field you specified will be res.partner, and if you selected a record and saved, suppose the id be 11 of model res.partner.

Later, in the second record, you choose the model as sale.order , so the co-model for the dynamic field will be now sale.order, and if you choose record and saved, let it be with ID 10. Now what happens is that, in the first record, you will have sale.order(11) instead of res.partner(11).

So, I don't think this will work out.

If you really want to implement this and if you have only need to select a few models, make many2one fields of necessary models and try to hide the unwanted many2one field based on the condition.

Thanks

形象
丢弃
编写者

Not that problem. actually i want to use in wizard to prepare some data based on selected model and record. So at a time user can select only one model and one record and after second time user open wizard then other model and other records. So not problem of model change. but question is how to achieve it?

相关帖文 回复 查看 活动
3
7月 24
4415
1
9月 23
6351
1
1月 22
11976
8
11月 16
3953
2
2月 25
5793