如何在创建方法中更新 One2many 字段?

huangapple go评论81阅读模式
英文:

How can i update One2many field on Create method?

问题

class DownloadReport(models.Model):
    _name = "santex.download_report"

    agency = fields.Many2one("res.partner", string="Agency",
                             domain="[('is_company', '=', True), '|', ('company_type_santex','=','Supplier/Vendor'), ('company_type_santex','=','Supplier Agency')]")

    report = fields.Binary(string="Report")
    file_name = fields.Text(string="Name", copy=False)
    customer_auth = fields.One2many("santex.customer_auth", inverse_name="company_skills")
    licensor_auth = fields.One2many("santex.licensor_auth", inverse_name="company_skills")
    certification_auth = fields.One2many("santex.certification_auth", inverse_name="company_skills")

    @api.model
    def create(self, vals_list):
        recs_1 = self.env['santex.customer_auth'].create({'agency': vals_list["agency"]})
        recs_2 = self.env['santex.licensor_auth'].create({'agency': vals_list["agency"]})
        recs_3 = self.env['santex.certification_auth'].create({'agency': vals_list["agency"]})

        vals_list["customer_auth"] = [(4, rec.id) for rec in recs_1]
        vals_list["licensor_auth"] = [(4, rec.id) for rec in recs_2]
        vals_list["certification_auth"] = [(4, rec.id) for rec in recs_3]

        record = super(DownloadReport, self).create(vals_list)

        return record

For information:

recs_1 = [santex.customer_auth(75,), santex.customer_auth(76,)]
    
recs_2 = [santex.licensor_auth(65,), santex.licensor_auth(66,)]
    
recs_3 = [santex.certification_auth(33,)]

Well, it seems like vals_list["customer_auth"] = rec_1 is wrong.

I am getting this error: psycopg2.ProgrammingError: can't adapt type 'santex.customer_auth'

Thanks for any help.


<details>
<summary>英文:</summary>

class DownloadReport(models.Model):
_name = "santex.download_report"

agency = fields.Many2one(&quot;res.partner&quot;, string=&quot;Agency&quot;,
                         domain=&quot;[(&#39;is_company&#39;, &#39;=&#39;, True), &#39;|&#39; ,(&#39;company_type_santex&#39;,&#39;=&#39;,&#39;Supplier/Vendor&#39;), (&#39;company_type_santex&#39;,&#39;=&#39;,&#39;Supplier Agency&#39;)]&quot;)

report = fields.Binary(string=&quot;Report&quot;)
file_name = fields.Text(string=&quot;Name&quot;, copy=False)
customer_auth = fields.One2many(&quot;santex.customer_auth&quot;, inverse_name=&quot;company_skills&quot;)
licensor_auth = fields.One2many(&quot;santex.licensor_auth&quot;, inverse_name=&quot;company_skills&quot;)
certification_auth = fields.One2many(&quot;santex.certification_auth&quot;, inverse_name=&quot;company_skills&quot;)

@api.model
def create(self, vals_list):
    recs_1 = self.env[&#39;santex.customer_auth&#39;].create({&#39;agency&#39;: vals_list[&quot;agency&quot;]})
    recs_2 = self.env[&#39;santex.licensor_auth&#39;].create({&#39;agency&#39;: vals_list[&quot;agency&quot;]})
    recs_3 = self.env[&#39;santex.certification_auth&#39;].create({&#39;agency&#39;: vals_list[&quot;agency&quot;]})

    vals_list[&quot;customer_auth&quot;] = recs_1
    vals_list[&quot;licensor_auth&quot;] = recs_2
    vals_list[&quot;certification_auth&quot;] = recs_3

    record = super(DownloadReport, self).create(vals_list)

    return record

For information: 
recs_1 = [santex.customer_auth(75,), santex.customer_auth(76,)]

recs_2 = [santex.licensor_auth(65,), santex.licensor_auth(66,)]

recs_3 = [santex.certification_auth(33,)]

Well, it seems like `vals_list[&quot;customer_auth&quot;] = rec_1` is wrong.

i am getting this error : **psycopg2.ProgrammingError: can&#39;t adapt type &#39;santex.customer_auth&#39;**

Thanks for any help.












</details>


# 答案1
**得分**: 1

你遇到的错误是 psycopg2.ProgrammingError: 无法适应类型 'santex.customer_auth',这是因为你试图将记录集(rec_1、rec_2、rec_3)分配给vals_list字典,而vals_list不支持字段customer_auth、licensor_auth和certification_auth的这种数据类型。

你可以尝试以下方法来修复这个问题:

```python
class DownloadReport(models.Model):
    _name = "santex.download_report"

    agency = fields.Many2one("res.partner", string="Agency",
    domain="[('is_company', '=', True), '|', ('company_type_santex', '=', 'Supplier/Vendor'), ('company_type_santex', '=', 'Supplier Agency')]")
    
    report = fields.Binary(string="Report")
    file_name = fields.Text(string="Name", copy=False)
    customer_auth = fields.One2many("santex.customer_auth", inverse_name="company_skills")
    licensor_auth = fields.One2many("santex.licensor_auth", inverse_name="company_skills")
    certification_auth = fields.One2many("santex.certification_auth", inverse_name="company_skills")
    
    @api.model
    def create(self, vals_list):
        recs_1 = self.env['santex.customer_auth'].create({'agency': vals_list["agency"]})
        recs_2 = self.env['santex.licensor_auth'].create({'agency': vals_list["agency"]})
        recs_3 = self.env['santex.certification_auth'].create({'agency': vals_list["agency"]})
 
        vals_list["customer_auth"] = [(4, rec.id) for rec in recs_1]
        vals_list["licensor_auth"] = [(4, rec.id) for rec in recs_2]
        vals_list["certification_auth"] = [(4, rec.id) for rec in recs_3]
 
        record = super(DownloadReport, self).create(vals_list)
 
        return record

希望对你有帮助。

英文:

The error you're encountering, psycopg2.ProgrammingError: can't adapt type 'santex.customer_auth', occurs because you're trying to assign a recordset (rec_1, rec_2, rec_3) to the vals_list dictionary, which is not a supported data type for the fields customer_auth, licensor_auth, and certification_auth.

You can try this to fix this:

class DownloadReport(models.Model):
    _name = &quot;santex.download_report&quot;

    agency = fields.Many2one(&quot;res.partner&quot;, string=&quot;Agency&quot;,
    domain=&quot;[(&#39;is_company&#39;, &#39;=&#39;, True), &#39;|&#39;, (&#39;company_type_santex&#39;, &#39;=&#39;, &#39;Supplier/Vendor&#39;), (&#39;company_type_santex&#39;, &#39;=&#39;, &#39;Supplier Agency&#39;)]&quot;)

    report = fields.Binary(string=&quot;Report&quot;)
    file_name = fields.Text(string=&quot;Name&quot;, copy=False)
    customer_auth = fields.One2many(&quot;santex.customer_auth&quot;, inverse_name=&quot;company_skills&quot;)
    licensor_auth = fields.One2many(&quot;santex.licensor_auth&quot;, inverse_name=&quot;company_skills&quot;)
    certification_auth = fields.One2many(&quot;santex.certification_auth&quot;, inverse_name=&quot;company_skills&quot;)
    @api.model
    def create(self, vals_list):
        recs_1 = self.env[&#39;santex.customer_auth&#39;].create({&#39;agency&#39;: vals_list[&quot;agency&quot;]})
        recs_2 = self.env[&#39;santex.licensor_auth&#39;].create({&#39;agency&#39;: vals_list[&quot;agency&quot;]})
        recs_3 = self.env[&#39;santex.certification_auth&#39;].create({&#39;agency&#39;: vals_list[&quot;agency&quot;]})
 
        vals_list[&quot;customer_auth&quot;] = [(4, rec.id) for rec in recs_1]
        vals_list[&quot;licensor_auth&quot;] = [(4, rec.id) for rec in recs_2]
        vals_list[&quot;certification_auth&quot;] = [(4, rec.id) for rec in recs_3]
 
        record = super(DownloadReport, self).create(vals_list)
 
        return record

Thanks

答案2

得分: 1

不需要更新一对多字段("customer_auth"、"licensor_auth" 和 "certification_auth")。您只需在 "create" 方法内为反向字段 "company_skills" 分配一个值。

@api.model
def create(self, vals_list):
    new_record = super(DownloadReport, self).create(vals_list)
    self.env['santex.customer_auth'].create({'agency': vals_list["agency"], 'company_skills': new_record.id})
    self.env['santex.licensor_auth'].create({'agency': vals_list["agency"], 'company_skills': new_record.id})
    self.env['santex.certification_auth'].create({'agency': vals_list["agency"], 'company_skills': new_record.id})
    return new_record
英文:

There is no need to update the one2many fields ("customer_auth", "licensor_auth", and "certification_auth"). You just need to assign a value to the inverse field "company_skills" inside the "create" method.

    @api.model
    def create(self, vals_list):
        new_record = super(DownloadReport, self).create(vals_list)
        self.env[&#39;santex.customer_auth&#39;].create({&#39;agency&#39;: vals_list[&quot;agency&quot;], &#39;company_skills&#39;: new_record.id})
        self.env[&#39;santex.licensor_auth&#39;].create({&#39;agency&#39;: vals_list[&quot;agency&quot;], &#39;company_skills&#39;: new_record.id})
        self.env[&#39;santex.certification_auth&#39;].create({&#39;agency&#39;: vals_list[&quot;agency&quot;], &#39;company_skills&#39;: new_record.id})
        return new_record

huangapple
  • 本文由 发表于 2023年5月24日 20:51:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定