# How to set selection values in a wizard state?

**URL:** <https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164>\
**Category:** Developer\
**Created:** [April 8, 2024, 5:12pm UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164 "2024-04-08T17:12:56Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)\
**Post date:** [April 8, 2024, 5:12pm UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/1 "2024-04-08T17:12:56Z")

</div>

I’m creating a wizard for importing Excel files. After the file has been uploaded (state `start`, user shall select the sheet to import (state `query_sheetname`).

How can I set the values to be available for selection in the `sniff` transition below?

Relevant parts:

```python
class ImportWizardQuerySheetname(ModelView):
    …
    sheet = fields.Selection((), "Sheet to import")

class ImportWizard(Wizard):
    …
    def transition_sniff(self): # the state-transition after "start"
            self.xlsx_reader = my_xlsx.xlsx_reader(self.start.file_)
            if self.xlsx_reader.sheetnames():
                # set the sheet names here so the user can select one.
                self.query_sheetname = [
                    (n, n) for n in self.xlsx_reader.sheetnames()]
                return 'query_sheetname'
            else:
                return 'import_'

```

---

<div class="post-metadata">

**Author:** ![yangoon](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/y/67e7ee/32.png) [@yangoon](https://discuss.tryton.org/u/yangoon)\
**Post date:** [April 9, 2024, 7:16am UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/2 "2024-04-09T07:16:40Z")

</div>

I think if you found more than one sheet in transition\_sniff you should just display another StateView and use the values from the input. The [initial configuration wizard](https://foss.heptapod.net/tryton/tryton/-/blob/branch/default/trytond/trytond/ir/module.py?ref_type=heads#L367) should provide you with some examples.

HTH

---

<div class="post-metadata">

**Author:** ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)\
**Post date:** [April 9, 2024, 12:51pm UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/3 "2024-04-09T12:51:01Z")

</div>

> [@yangoon](#):
>
> I think if you found more than one sheet in transition\_sniff you should just display another StateView and use the values from the input.

Thx. Of course, that’s the idea 🙂

The initial configuration wizard seems to “just” get the next config wizard and executes that. Which does not match my problem (Please correct my if I’m wrong). My specific issue is: How to set the selectable values for this next StateView.

Meanwhile I tried:

- Passing a method name as first parameter to `fields.Selection()`. The method depends on a hidden field (which I wanted to use to pass the table names around):

- Setting value and default for `sheet` in the wizard, using `value_…` and `default_…`.  
This did not succeed since the value for `sheet2` set in the `value_…` method did not make it to `get_sheetnames()` either.

#### The Solution I Found

Thus I ended up with defining a subclass of `StateView` which hacks the selectable values:

```auto
class QuerySheetnameStateView(StateView):
    def get_view(self, wizard, state_name):
        view = super().get_view(wizard, state_name)
        sheetnames = wizard.row_reader.sheetnames()
        if sheetnames:
            view['fields']['sheet']['selection'] = [
                (n, n) for n in sheetnames
            ]
        return view

```

and using this subclass:

```auto
class ImportWizard(Wizard):
            query_sheetname = QuerySheetnameStateView(…)

```

I someone knows a better solution, I’d be happy to learn about.

---

<div class="post-metadata">

**Author:** ![yangoon](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/y/67e7ee/32.png) [@yangoon](https://discuss.tryton.org/u/yangoon)\
**Post date:** [April 9, 2024, 2:17pm UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/4 "2024-04-09T14:17:41Z")

</div>

> [@htgoebel](#):
>
> The initial configuration wizard seems to “just” get the next config wizard and executes that. Which does not match my problem (Please correct my if I’m wrong). My specific issue is: How to set the selectable values for this next StateView.

My idea to set selectable values was in the way like the selection of languages works. Did you see the according [StateView](https://foss.heptapod.net/tryton/tryton/-/blob/branch/default/trytond/trytond/ir/lang.py?ref_type=heads#L617) and [Wizard extension](https://foss.heptapod.net/tryton/tryton/-/blob/branch/default/trytond/trytond/ir/lang.py?ref_type=heads#L630) and [integration as wizard item](https://foss.heptapod.net/tryton/tryton/-/blob/branch/default/trytond/trytond/ir/lang.xml?ref_type=heads#L558)?

---

<div class="post-metadata">

**Author:** ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)\
**Post date:** [April 10, 2024, 8:42am UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/5 "2024-04-10T08:42:44Z")

</div>

Thanks for pointing to the concrete places. In deed I missed them.

This indeed looks tricky and at first glance promissing: A `Many2Many` field with `origin` and `target` set to `None`. 💯

Anyway, even when using a `One2Many` (which would match my case of selecting one sheet), would require creating a Model and - even worse - store the names of the sheets there. But the names are ephemeral. Thus this seems to not be the direction to go.

Did I miss something?

---

<div class="post-metadata">

**Author:** ![yangoon](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/y/67e7ee/32.png) [@yangoon](https://discuss.tryton.org/u/yangoon)\
**Post date:** [April 10, 2024, 12:21pm UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/6 "2024-04-10T12:21:58Z")

</div>

Wouldn’t it be possible to create a suitable selection on the fly and just use a selection field?

---

<div class="post-metadata">

**Author:** ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)\
**Post date:** [April 10, 2024, 1:10pm UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/7 "2024-04-10T13:10:15Z")

</div>

> [@yangoon](#):
>
> create a suitable selection on the fly and just use a selection field?

??? This - receptively the complications with this - is what I’m talking about in [How to set selection values in a wizard state? - #3 by htgoebel](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/3).

---

<div class="post-metadata">

**Author:** ![yangoon](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/y/67e7ee/32.png) [@yangoon](https://discuss.tryton.org/u/yangoon)\
**Post date:** [April 10, 2024, 2:11pm UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/8 "2024-04-10T14:11:14Z")

</div>

Perhaps I didn’t read close enough, I was somewhat confused by the representation of the code and what belongs to which class.

I don’t know if you have to go over get\_view of StateView to address the selection field. If you define the selection field directly on the according ModelView it doesn’t work per by providing a function? At least I think you could address the field directly on the ModelView. If not then I am happy to have learned your solution 😉 .

---

<div class="post-metadata">

**Author:** ![udono](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/udono/32/2114_2.png) [@udono](https://discuss.tryton.org/u/udono)\
**Post date:** [April 10, 2024, 4:06pm UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/9 "2024-04-10T16:06:19Z")

</div>

Why not use [trytond.model.fields.Selection.selection\_change\_with](https://docs.tryton.org/latest/server/ref/fields.html#trytond.model.fields.Selection.selection_change_with) and create the selection list on the fly in the same view where you upload the file?

---

<div class="post-metadata">

**Author:** ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)\
**Post date:** [April 11, 2024, 1:39pm UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/10 "2024-04-11T13:39:11Z")

</div>

Thanks for this tip. The main reason I did not use it is that I didn’t know about it and there ist no tutorial. (BTW: The `selection_change_with` parameter is deprecated.)

I made a test with this and it works quite well. I really like it and it’s very useful.

But for this use-case it has one major deficit for this use-case: The file will be transferred several time from the client to the server: once when the `selection_change_with` triggers and the selection gets updated. And a second time when the from is completed.

This is not an issue if the file is rather small and the server is in the LAN. Anyway, I’m afraid performance will collapse if the file is 1 MB and the server is somewhere remote.

WDYT?

---

<div class="post-metadata">

**Author:** ![udono](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/udono/32/2114_2.png) [@udono](https://discuss.tryton.org/u/udono)\
**Post date:** [April 15, 2024, 7:39am UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/11 "2024-04-15T07:39:00Z")

</div>

IIUC you are looking for [the example in the documentation](https://docs.tryton.org/latest/server/topics/wizard.html#wizard).

---

<div class="post-metadata">

**Author:** ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)\
**Post date:** [April 15, 2024, 7:58am UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/12 "2024-04-15T07:58:14Z")

</div>

> [@udono](#):
>
> IIUC you are looking for [the example in the documentation](https://docs.tryton.org/latest/server/topics/wizard.html#wizard).

No. I know this example, which shows a basic wizard only. By no means it covers any of the questions in this thread.

---

<div class="post-metadata">

**Author:** ![udono](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/udono/32/2114_2.png) [@udono](https://discuss.tryton.org/u/udono)\
**Post date:** [April 15, 2024, 9:00am UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/13 "2024-04-15T09:00:29Z")

</div>

Look up line

```auto
    self.result.file = Translation.translation_export(

```

There in the transition we set the variable `file` in the coming `StateView()` named `result`.  
The variable is re-used in the method:

```auto
    def default_result(self, fields):

```

IMHO it should be possible to set the [`selection function or list of tuples`](https://docs.tryton.org/latest/server/ref/fields.html#trytond.model.fields.Selection.selection) of your selection also in a similar way.  
Maybe there is an invisible `fields.Char` needed to utilize the `default_` mechanism in combination with `selection on_change_with`, but I am not sure.

---

<div class="post-metadata">

**Author:** ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)\
**Post date:** [April 15, 2024, 9:54am UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/14 "2024-04-15T09:54:41Z")

</div>

Which of the issue in this thread you are referring to?

I assume you referring to

> [@htgoebel](#):
>
> The file will be transferred several time from the client to the server:

The wizard example is missing this point, as it creates a file _server-side_ which it _server-side_ forward to the next state where it is transferred once to the client.

My quote refers to `selection_change_with` transferring the file from the client to the server. And the same file is transferred again from the client to the server again when finishing the view.

If you are suggesting to just a second stateview for asking the user for the sheetname: This is what I already implemented at the very beginning,

---

<div class="post-metadata">

**Author:** ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)\
**Post date:** [April 16, 2024, 3:02pm UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/15 "2024-04-16T15:02:53Z")

</div>

@yangoon I published the code and a first release. The code we have been taking about is in [this commit](https://foss.heptapod.net/tryton-community/incubator/importwizard/-/commit/7b6abce6e75431dda1aa2abe82793ca9f4111046). Meanwhile I moved forward to using `selection_change_with` as Udo suggested. Thanks for sharing your thoughts.

---

<div class="post-metadata">

**Author:** ![yangoon](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/y/67e7ee/32.png) [@yangoon](https://discuss.tryton.org/u/yangoon)\
**Post date:** [April 16, 2024, 11:33pm UTC](https://discuss.tryton.org/t/how-to-set-selection-values-in-a-wizard-state/7164/16 "2024-04-16T23:33:23Z")

</div>

Congrats! So it seems that a selection method works together with an _ordinary 😉_ on\_change\_with. Good to know it works.
