# \[Wizard\] Create Many2One domain on One2Many in the same form

**URL:** <https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872>\
**Category:** Developer\
**Created:** [November 12, 2018, 12:36pm UTC](https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872 "2018-11-12T12:36:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [November 12, 2018, 12:36pm UTC](https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872/1 "2018-11-12T12:36:19Z")

</div>

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?

The code:

> documents = fields.One2Many(‘document.document’, ‘resources’, ‘Additional Documents’)  
> calibration\_cert = fields.Many2One(‘document.document’, ‘Calibration certificate’,  
> domain=[  
> (‘type’, ‘=’, ‘calibration\_cert’),  
> (‘id’, ‘in’, Eval(‘documents’, )),  
> ],  
> depends=[‘documents’],  
> required=True,  
> help=“Select a document as calibration certificate.”)

I can add the documents, but the selection of the field calibration\_cert stays empty.

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [November 12, 2018, 1:03pm UTC](https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872/2 "2018-11-12T13:03:26Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [November 12, 2018, 3:59pm UTC](https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872/3 "2018-11-12T15:59:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [November 13, 2018, 9:47am UTC](https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872/4 "2018-11-13T09:47:20Z")

</div>

If you do not store the records of One2Many, I do not see how you could make reference to them as they will not have a unique id.

---

<div class="post-metadata">

**Author:** ![albert](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/albert/32/21_2.png) [@albert](https://discuss.tryton.org/u/albert)\
**Post date:** [November 13, 2018, 10:48am UTC](https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872/5 "2018-11-13T10:48:51Z")

</div>

> [@edbo](#):
>
> Would it be possible to create a selection field and fill that dynamically with the list of documents from the One2Many?

I guess you can’t without a hack.

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.

---

<div class="post-metadata">

**Author:** ![mono](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/mono/32/289_2.png) [@mono](https://discuss.tryton.org/u/mono)\
**Post date:** [November 13, 2018, 4:50pm UTC](https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872/6 "2018-11-13T16:50:55Z")

</div>

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")

```

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [November 13, 2018, 6:37pm UTC](https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872/7 "2018-11-13T18:37:22Z")

</div>

Such validation is OK for ModelView only. If it is stored in the database, you must use a `UNIQUE` SQL constraint to work with concurrency.

---

<div class="post-metadata">

**Author:** ![mono](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/mono/32/289_2.png) [@mono](https://discuss.tryton.org/u/mono)\
**Post date:** [November 13, 2018, 8:23pm UTC](https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872/8 "2018-11-13T20:23:35Z")

</div>

> [@ced](#):
>
> you must use a `UNIQUE` SQL constraint

but I had problems with Unique constraint when a resource have more than 2 documents:

Unique(cls. **table** (), table.document,table.calibration\_cert)

- Document: 1, **resource: 1** , calibration\_cert = **False**
- Document: 2, resource: 1, calibration\_cert = True
- Document: 3, **resource: 1** , calibration\_cert = **False**  
Unique constraint fail

but the validation can be put on ModelStorage.validate or write method.

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [November 13, 2018, 9:07pm UTC](https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872/9 "2018-11-13T21:07:44Z")

</div>

Yes you are right. It is not [Unique](http://docs.tryton.org/projects/server/en/latest/ref/models/models.html#unique) but [Exclude](http://docs.tryton.org/projects/server/en/latest/ref/models/models.html#exclude) that should be used for partial constraint.

---

<div class="post-metadata">

**Author:** ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)\
**Post date:** [November 20, 2018, 9:02am UTC](https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872/10 "2018-11-20T09:02:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)\
**Post date:** [December 20, 2018, 9:02am UTC](https://discuss.tryton.org/t/wizard-create-many2one-domain-on-one2many-in-the-same-form/872/11 "2018-12-20T09:02:47Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
