Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
2 Replies
949 Tampilan

Hello,

as the title mentions, I pass the a variable from purchase order form using a button:

<xpath expr="//button[@name='print_quotation']" position="after">

                <button name="%(dynamic_reporting.action_report_selection_wizard)d"

                    string="Print Custom Report"

                    class="btn-primary"

                    type="action"

                    context="{'default_model_id': active_model}"

                    />

            </xpath>

The value is passed with no problem(in my case I get default_model_id is: 'purchase.order'), but when I try to use that default_model_id to get to the ID of the model:

@api.model

    def _default_model_id(self):

        active_model = self.env.context.get('default_model_id', '')

        return self.env['ir.model'].search([('model', '=', active_model)], limit=1).id

    model_id = fields.Many2one('ir.model',

            'Modèle',

            domain="[('transient', '=', False)]",

            default=_default_model_id

            )

the field model_id always stay empty. I tried a bunch of stuff but it didn't amount to anything


any help would be appreciated

Avatar
Buang
Jawaban Terbai

Hi,

Please refer to the code below:

Python:



class YourWizard(models.TransientModel):

    _name = 'your.wizard.model'

    _inherit = 'your.wizard.model'


    model_id = fields.Many2one(

        'ir.model',

        string='Modèle',

        compute='_compute_model_id',

        store=False

    )


    @api.depends_context('default_model_id')

    def _compute_model_id(self):

        for rec in self:

            model_str = self.env.context.get('default_model_id')

            model = self.env['ir.model'].search([('model', '=', model_str)], limit=1)

            rec.model_id = model



XML


<xpath expr="//button[@name='print_quotation']" position="after">

       <button name="%(dynamic_reporting.action_report_selection_wizard)d"

             string="Print Custom Report"

             class="btn-primary"

             type="action"

                context="{'default_model_id': model_id}"

        />

</xpath>




Hope it helps.

Avatar
Buang
Jawaban Terbai

In the button, you set the model_id as char() and then in the model it is Many2one which is confusing. By default, when you set the default_field_name, odoo will set the field name (without the prefix default_) to that value. If you want to pass the model id, simply set the default_model_id:active_model.id, or use another name for the context.

Avatar
Buang
Post Terkait Replies Tampilan Aktivitas
2
Jul 25
232
4
Jul 25
796
1
Jul 25
111
2
Jul 25
233
1
Jul 25
290