如何根据 sale_order_line 中的自定义日期时间字段更新 stock_move 文档中的 date_expected 值

How to update date_expected value in stock_move document based on custom datetime field in sale_order_line

我是 odoo 的新手。我正在尝试根据 sale_order_line 中的用户给定值(自定义字段)更新 stock_move 文档中的 date_expected 值。这是我在代码中尝试的内容:

class SOLModel(models.Model):
   _inherit = 'sale.order.line'

   date_expected = fields.Datetime('Expected Date')

   sale_delay=fields.Float(related='product_id.product_tmpl_id.sale_delay');

   @api.multi
   def _prepare_procurement_values(self, group_id=False):
       res = super(SOLModel, self)._prepare_procurement_values(group_id)
       date_expected_do = fields.Datetime.from_string(self.date_expected) - timedelta(days=int(self.sale_delay))
       res.update({'date_expected': date_expected_do})
       return res

class StockRuleInherit(models.Model):
   _inherit = 'stock.rule'

   def _get_stock_move_values(self, product_id, product_qty, product_uom, location_id, name, origin, values, group_id):
       res = super(StockRuleInherit, self)._get_stock_move_values(product_id, product_qty, product_uom, location_id,
                                                               name, origin, values, group_id)
       res['date_expected'] = values.get('date_expected', False)
       return res

这是我的 xml

<record model="ir.ui.view" id="sale_order_line_view_inherit">
     <field name="name">sale.order.line.view.inherit1</field>
     <field name="model">sale.order</field>
     <field name="inherit_id" ref="sale.view_order_form"/>
     <field name="arch" type="xml">
         <xpath expr="/form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='name']" position="after">
            <field name="date_expected" force_save="1" options="{'datepicker':{'minDate': 'now'}}"
             attrs="{'readonly': [('state', 'in', ('done', 'cancel'))],'required': True,}"/>
             <field name="sale_delay" invisible="1"/>
         </xpath>
     </field>
</record>

但是我在尝试 confirm/save 销售订单时遇到错误。

The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set

[object with reference: date_expected - date.expected]

在调试日志中我可以看到

 bad query: b'INSERT INTO "stock_move" ("id", "create_uid", "create_date", "write_uid", "write_date", "additional", "company_id", "date", "date_expected", "group_id", "is_done", "location_dest_id", "location_id", "name", "origin", "partner_id", "picking_type_id", "priority", "procure_method", "product_id", "product_uom", "product_uom_qty", "propagate", "rule_id", "scrapped", "sequence", "state", "to_refund", "warehouse_id") VALUES (nextval(\'stock_move_id_seq\'), 1, (now() at time zone \'UTC\'), 1, (now() at time zone \'UTC\'), false, 1, \'2019-09-12 11:49:04\', NULL, 1668, false, 15, 12, \'VF01: Output \xe2\x86\x92 Customers\', \'SO074\', 9620, 3, \'1\', \'make_to_order\', 23836, 1, \'1.000\', true, 4, false, 10, \'draft\', false, 1) RETURNING id'
ERROR: null value in column "date_expected" violates not-null constraint

有人可以帮我解决这个问题吗? 提前致谢。

根据您的日志,库存变动试图使用 date_expected 字段的空值创建,这是必需的。

这是您传递给股票移动创建方法的值

{
"id": "nextval('stock_move_id_seq')",
"create_uid": 1,
"create_date": "(now() at time zone 'UTC')",
"write_uid": 1,
"write_date": "(now() at time zone 'UTC')",
"additional": false,
"company_id": 1,
"date": "'2019-09-12 11:49:04'",
"date_expected": null,
"group_id": 1668,
"is_done": false,
"location_dest_id": 15,
"location_id": 12,
"name": "'VF01: Output \u00e2\u0086\u0092 Customers'",
"origin": "'SO074'",
"partner_id": 9620,
"picking_type_id": 3,
"priority": "'1'",
"procure_method": "'make_to_order'",
"product_id": 23836,
"product_uom": 1,
"product_uom_qty": "'1.000'",
"propagate": true,
"rule_id": 4,
"scrapped": false,
"sequence": 10,
"state": "'draft'",
"to_refund": false,
"warehouse_id": 1
}

可能的原因是,create 方法在某处被覆盖,从传递的 vals 中弹出 date_expected 值。

您还可以检查 stock.move 的创建方法以了解最终值的详细信息。

简答:您的直接问题似乎是当您调用 _get_stock_move_values 时,values 没有 expected_date 所以 values.get('date_expected', False) 的计算结果为 False,当尝试存储到数据库中时,它又被转换为 NULL

那么,也许你重载了错误的 _prepare_procurement_values,因为它们有四个:

<path_to_v12>/addons/sale/models/sale.py:1429:    def _prepare_procurement_values(self, group_id=False):
<path_to_v12>/addons/sale_stock/models/sale_order.py:325:    def _prepare_procurement_values(self, group_id=False):
<path_to_v12>/addons/stock/models/stock_warehouse.py:971:    def _prepare_procurement_values(self, product_qty, date=False, group=False):
<path_to_v12>/addons/stock/models/stock_move.py:781:    def _prepare_procurement_values(self):

我还可以建议将字段名称从 expected_date 更改为 expected_date_custom 之类的名称,因为您正在使用的函数会生成许多 expected_date 值。每个实例:

196     def _get_stock_move_values(self, product_id, product_qty, product_uom, location_id, name, origin, values, group_id):
...
203         date_expected = fields.Datetime.to_string(
204             fields.Datetime.from_string(values['date_planned']) - relativedelta(days=self.delay or 0)
205         )
...
209         move_values = {
...
226             'date': date_expected,
227             'date_expected': date_expected,
...
230         }
...
234         return move_values

写入数据库时​​ False 如何结束 NULL

<path_to_v12>/odoo/models.py

3361     def _write(self, vals):
3362         # low-level implementation of write()
...
3379         for name, val in vals.items():
...
3386             if field.column_type:
...
3389                     val = field.convert_to_column(val, self, vals)

<path_to_v12>/odoo/fields.py

755     def convert_to_column(self, value, record, values=None):
756         """ Convert ``value`` from the ``write`` format to the SQL format. """
757         if value is None or value is False:
758             return None
...