python-3.x - Odoo:在创建时在另一个模型的 many2one 字段中保存当前 ID
问题描述
我正在尝试做的是我正在custom.modify.price
为产品创建一个模型,我可以在其中选择特定的标准来选择产品,然后我填写一个影响所有先前选择的价格的百分比. 我希望它为选择中的每个产品,在另一个模型中创建一个新记录,custom.sale.price
并且每个记录应该保存在字段中创建它ID
的记录。问题是我可以通过覆盖该方法并为其提供值来创建一个新记录,但我无法为其提供当前“尚未创建”的记录custom.modify.price
many2one
custom.sale.price
custom.modify.price
create
ID
custom.modify.price
class CustomModifyPrice(models.Model):
date = fields.Datetime(string='Date', required=True, default=fields.Datetime.now())
brand_id = fields.Many2many(comodel_name="custom.brand", string="Brand", required=False, )
origin_id = fields.Many2many(comodel_name="custom.country", string="Origin", required=False, )
percentage = fields.Float(string="Percentage")
product_ids = fields.Many2many(comodel_name="custom.product", string="Products", readonly=True, )
@api.model
def create(self, vals):
values = {'product_id': product.id, 'date': vals['date'], 'sell_price': mod_sell_price, 'modification_id': self.id}###self.id returns nothing here
price = self.env['custom.sale.price'].create(values)
result = super(CustomModifyPrice, self).create(vals)
return result
class CustomSalePrice(models.Model):
product_id = fields.Many2one(comodel_name="custom.product", string="Product", required=False, )
date = fields.Datetime(string="Date", default=fields.Datetime.now())
sell_price = fields.Float(string="Selling Price")
sale_id = fields.Many2one(comodel_name="custom.sale", string="Sales Order", required=False, )
解决方案
您需要custom.modify.price
使用结果创建和更新值,id
然后创建custom.sale.price
@api.model
def create(self, vals):
result = super(CustomModifyPrice, self).create(vals)
values = {'modification_id': result.id, ...}
self.env['custom.sale.price'].create(values)
return result
推荐阅读
- javascript - 尝试使用 nodemailer 在 nodejs 中设置密码恢复
- java - 如果硒中有可用的结果列表,如何检查屏幕是否需要向下滚动以查看搜索结果
- python - 如何比较两个数据框熊猫
- swift - SwiftUI TabView + NavigationView 导航栏不显示
- jquery - ISO 日期格式显示 RangeError
- text - ASCII 还是 UTF-8?
- python - 如何为 SQL 查询值使用列表?
- hiveql - 将动态数组行数据拆分为配置单元中的列名
- javascript - 基于 parentId 和 id 属性嵌套平面对象
- python-3.x - 使用 fmin() 找到最小值