如何找出用户更改字段和字段从Onchange函数中的表中自动填充的何时自动填充的区别?

发布于 2025-01-23 15:57:46 字数 735 浏览 6 评论 0原文

我有一个许多2ONE字段作为A_Details A_Details在A上具有过滤器基础,

A = fields.Many2one(comodel_name="headertable")
A_details = fields.One2many(comodel_name="detailtable")
              

并且在XML中,我将A_Detail中的上下文传递给A_Detail中的A_Details,

<field name="A_detail" context="{'parent_id': A,}"/>

现在我想在用户更改A_Detail的记录中,然后将用户更改A_Detail的记录。因此,我在这样的字段上使用OnChange Decorator:

@api.onchange('A')
def _delete_selected_records(self):
    for rec in self.A_details:
            self.A_details = [(3, rec.id, 0)]    

此功能正常工作以创建模式,但是当我从树视图打开记录时发生问题,而当一个字段从模型上获得值,请从模型上获得值删除所有a_detail的记录,这就是为什么我要检查此功能,如果用户更改a,请删除a_detail的记录,如果系统设置为字段的值无需执行 ...我该如何处理此问题?

I have a many2one field as A and one2many field as A_details that A_details has filter base on A,

A = fields.Many2one(comodel_name="headertable")
A_details = fields.One2many(comodel_name="detailtable")
              

and in the xml I pass the A value with context in A_detail to filter it

<field name="A_detail" context="{'parent_id': A,}"/>

now I want to delete A_detail's records when user changes the A value, so I use onchange decorator on A field like this:

@api.onchange('A')
def _delete_selected_records(self):
    for rec in self.A_details:
            self.A_details = [(3, rec.id, 0)]    

this function workes correctly to create mode but the problem occurs when I open the record from tree view and while A field is getting value from model Onchanged decorator call the _delete_selected_records function and it delete all the A_detail's records, That's why I want to check in this function that if user change the A, delete the A_detail's records else if system sets value to field do nothing... how can I handle this???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ペ泪落弦音 2025-01-30 15:57:46

如果A被删除,则应添加检查 ,如果a是false ,则 do x

    @api.onchange('A')
    def _delete_selected_records(self):
        for rec in self:
           if rec.A is False:
              rec.A_details = [(3, rec.id, 0)] # You can you .unlink() btw
           else:
              continue 

即使函数是由功能触发的,即使假设用户意外地将光标放在视图中的场上,或者您不会冒险删除任何内容,除非满足正确的条件

You should add a check if A is deleted or not meaning if A is False then do X:

    @api.onchange('A')
    def _delete_selected_records(self):
        for rec in self:
           if rec.A is False:
              rec.A_details = [(3, rec.id, 0)] # You can you .unlink() btw
           else:
              continue 

This way even if the function is triggered by let's say a user accidentally putting cursor on the field in the view or something you won't risk deleting anything unless the correct condition is met.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文