Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0] When you open a form view by python, default values for an one2many field cannot be retrived if these fields are not defined in tree view #117122

Open
lihaotong100 opened this issue Mar 30, 2023 · 1 comment
Labels
16.0 ORM ORM, python Framework related

Comments

@lihaotong100
Copy link
Contributor

lihaotong100 commented Mar 30, 2023

Impacted versions:
16.0
Steps to reproduce:

class PurchaseRequisition(models.Model):
    _name = 'purchase.requisition'
    line_ids = fields.One2many('purchase.requisition.line', 'requisition_id')

class PurchaseRequisitionLine(models.Model):
    _name = 'purchase.requisition.line'
    product_name = fields.Char()
    product_type_1 = fields.Selection([('Normal', 'Normal'), ('Special', 'Special')])
    product_type_2 = fields.Char()
    state = fields.Selection(PurchaseRequisitionLineStateEnum.ALL)

in view, define your one2many field and add the default attribute with the default values you want to set:

<field name="line_ids">
    <tree>
        <field name="product_name"/>
    </tree>
</field>

create a button to open a form with an one2many field and set the values for this field in the context

def open_form_view(self, **kw):
    default_line_ids = []
    default_line_ids.append((0, 0, {
        'product_name': "test",
        "product_type_1": 'Normal',
        "product_type_2": '12321',
        "state": PurchaseRequisitionLineStateEnum.NEW.value,
    }))
    return {
                "type": "ir.actions.act_window",
                "res_model": "purchase.requisition",
                "view_mode": "form",
                "context": dict(self._context, default_line_ids=default_line_ids),
            }

Current behavior:
Only the fields of one2many occurring in the tree view(product_name) can be assigned with the default values.

Expected behavior:
All fields in the one2many fields can be assigned

Video/Screenshot link (optional):

Support ticket number submitted via odoo.com/help (optional):

@vava-odoo vava-odoo added ORM ORM, python Framework related 16.0 labels Apr 4, 2023
@razvanioan
Copy link

Hi @lihaotong100

Any updates on this ?
It seems that this is still happening in v17 as well

I've hit this one by simply trying to use default_item_ids (Pricelist Rules) on the product.pricelist model (using advanced pricelists) and I did not understand why only some values where being set and the others not ...

Here's a short video that I've just recorded on latest v17 Odoo Runbot where I'm trying to create a pricelist with one rule applied on a specific product, with 40% discount, and you'll notice that:

  • the percent_price is not set with the desired value, but default 0
  • the product shows up as Unnamed (until I save the pricelist)
  • if I would have set product variant instead, it would have not been set as well, and when trying to save the pricelist the required field validation warning would have popped up
2023-12-19_21-00-02.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
16.0 ORM ORM, python Framework related
Projects
None yet
Development

No branches or pull requests

3 participants