Help with context of StateView model

I am implementing a party creation wizard, loading the data, and opening the form with:

class MyWizard(Wizard):
    "My Wizard"
    __name__ = 'xxx'

    start = StateTransition()
    create_party = StateView('party.party', 'party.party_view_form', [
            Button("Cancel", 'end', 'tryton-cancel'),

    def value_create_party(self, fields):
        pass

The problem is that I want a specific company to appear in the party context. (For Multivalue)
Is this possible to do? Or is it a missing feature?

Doing it this way works, but I want to do it individually.

class PartiesWizard(StateView):
    "Parties Wizard"
    __name__ = 'party.parties_wizard'

    company = fields.Many2One('company.company', ...)
    parties = fields.One2Many('party.party', None, "Parties",
        context={
            'company': Eval('company', None),
            },
        depends={'company'})


class MyWizard(Wizard):
    "My Wizard"
    __name__ = 'xxx'

    start = StateTransition()
    create_parties = StateView('party.parties_wizard', 'xxxxxxx', [
            Button("Cancel", 'end', 'tryton-cancel'),

    def value_create_parties(self, fields):
        pass

A wizard can not change its own execution context.
But you could have a first wizard that ask a company and then launch a second wizard with the selected company in the context using a StateAction and a do_ method.

Thank you for your quick response, but it doesn’t seem to be possible. I’ve tried it, and it’s the client who launches the action with the current context in this

The context can be set by the wizard StateAction method.

    def do_xxx(self, action):
        ...
        context = Transaction().context
        action['pyson_context'] = PYSONEncoder().encode({
                'company': company.id,
                })
        with Transaction().set_context(company=company.id):
            return action, {
                'company': company.id,
                'id': context.get('active_id'),
                'ids': context.get('active_ids'),
                'model': context.get('active_model'),
                }

I have tried the following methods. Which one do you think is correct?

From what I have seen in the code, context is not taken into account.
However, I have seen that pyson_context is used for case ‘ir.action.act_window’:.

You are right. There is only context for window actions.
Indeed it would make sense to add it also to wizard and report.

For now I see no other options than opening a form instead of a wizard.
Using wizard to create record is anyway not the Tryton way.

Okay, thank you very much, that will be a good feature.

  • Using wizard to create record is anyway not the Tryton way.*

This is a pre-assisted process that loads data from an external source, based on information from a record that contains information by company. I don’t see any other way.

This sounds like autocomplete from external source similar to VIES.

Yes, I have analyzed and tested this option, but it does not fit my needs.
If I am not mistaken, this can only be triggered in an m2o, m2m… with user interaction.

Yes but you could create a wizard with a Many2One for example. This has the benefit to prevent creating duplicates.

9 posts were split to a new topic: Use autocompletion from main model

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