Skip to main content
 首页 » 编程设计

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

2024年11月01日8cyq1162

我是 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> 

但是我在尝试确认/保存销售订单时遇到错误。

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 

谁能帮我解决这个问题? 提前致谢。

请您参考如下方法:

简短回答:您的直接问题似乎是当您调用 _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 
...