This question has been flagged
3 Replies
4293 Views

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

Avatar
Discard
Best Answer

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

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


Avatar
Discard
Best Answer

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


Avatar
Discard
Author

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

Best Answer

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

Avatar
Discard
Author

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?