I have a wizard with a One2Many field. A user adds data to that field. After that the user have to do one step more and should select one item from the list created with the One2Many. How is this possible?
Of course, the record created in the One2Many do not exist in the database so the search on the Many2One can not find them. Your pre-selection of record should be based on already saved record (e.g. with a Many2Many). Or you must use something else to find the specific record (e.g. you take the first one).
Would it be possible to create a selection field and fill that dynamically with the list of documents from the One2Many? I created a second form, so the user first adds the documents and clicks on “next”. After that in the second form the user gets the possibility to select the right document.
I think that the most you can do is add a label that tells the user that she must save before they can use newly created records of the O2M. Or as Cédric suggests, use the order of the records. That is not a possibility, however when the value must depend on company, for example.
What we do in the only case we need something like that is to teach the user they must save before picking the records in the m2o field.
I had a similar situation then I added a boolean field to document.document (calibration_cert) and I validated that the resource has one, and only one, document type calibration_cert:
class Document(ModelSQL,ModelView):
"""Document Model """
__name__ = 'document.document'
...
...
calibration_cert = fields.Boolean('Calibration Cert')
class YourModel(ModelSQL,ModelView):
"""Your Model"""
documents = fields.One2Many(‘document.document’, ‘resources’, ‘Additional Documents’)
calibration_cert = fields.Function(Fields.Many2One(),'get_calibration_cert')
def get_calibration_cert(self):
if self.documents:
for document in self.documents:
if document.calibration_cert:
return document.id
return None
def pre_validate(self):
calibration_certs = 0
for document in self.documents:
if document.calibration_cert:
calibration_certs += 1
if calibration_certs != 1:
self.raise_user_error("Error Only one document type calibration cert")
Just to mention, it’s a Wizard and indeed it’s not possible (yet?) to set a domain based on a other field in the wizard.
I have tried to get the filename in a Selection, so the user can select the name, but that also didn’t work. Added a “next” transition also no luck.
I ended up by letting the user fill in as much as possible in the Wizard and then the last action is a StateAction, which opens the completed calibration, so the user can kind of fine tune the data and then close it.