one2many only allow one record

Asked by jerryzhang

I want to create my own class and use in system class like res.partner and crm.lead, I use the one2many to reach it, like res.partner and res.partner.address, but i want to only allow create one record for my own class, and now I can create some address for one partner, should I reach it like this:
     also use one2many, but only allow create one address for a partner and hide the Add button on views.

Question information

English Edit question
Odoo Server (MOVED TO GITHUB) Edit question
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Launchpad Janitor (janitor) said :

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
Kalpana Hemnani(SerpentCS) (khemnani-serpentcs) said :

Hello jerryzhang,

If you want to allow only one one2many record, you should use many2one.

You can create your own class and using that class in res.partner by many2one as it will allow you to insert only one record and it won't display add button in view instead it will display magnifying glass.

In case you stick to on2many, you have these 2 options:
1. Add a constraint(custom one) to block the insertion of more than one O2M records.
2. Over the xml file, set widget=many2one for the o2m field.

To hide the 'Add' button, here are the probables:
1. Program the client(GTK/Web) accordingly.
2. Access : Bare the create access of the one2many model and you will never be able to create the record.

Thank you,
Kalpana Hemnani,
Serpent Consulting Services.

Revision history for this message
jerryzhang (jerry-zhangzhe) said :

thanks Kalpana