考虑以下四种模式:
class bsi_production_order(models.Model):
_name = 'bsi.production.order'
name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
date = fields.Date(string="Production Date")
production_type = fields.Selection([
('budgeted','Budgeted'),
('nonbudgeted','Non Budgeted'),
('direct','Direct Order'),
], string='Type of Order', index=True,
track_visibility='onchange', copy=False,
help=" ")
notes = fields.Text(string="Notes")
order_lines = fields.One2many('bsi.production.order.lines', 'production_order', states={'finished': [('readonly', True)], 'cancel': [('readonly', True)]}, string="Order lines", copy=True)
print_orders = fields.One2many('bsi.print.order', 'production_orders', string="Print Orders")
class bsi_production_order_lines(models.Model):
_name = 'bsi.production.order.lines'
production_order = fields.Many2one('bsi.production.order', string="Production Orders")
isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
qty = fields.Float(string="Quantity")
consumed_qty = fields.Float(string="Consumed quantity")
remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func")
@api.onchange('qty', 'consumed_qty')
def _remaining_func(self):
if self.qty or self.consumed_qty:
self.remaining_qty = self.qty +(-self.consumed_qty)
class bsi_print_order(models.Model):
_name = 'bsi.print.order'
name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
date = fields.Date(string="Print Date")
origin = fields.Char(string="Origin")
production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
order_lines = fields.One2many('bsi.print.order.lines', 'print_order', string="Order lines")
class bsi_print_order_lines(models.Model):
_name = 'bsi.print.order.lines'
print_order = fields.Many2one('bsi.print.order', string="Print Order")
production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
qty = fields.Integer(string="Quantity")
consumed_qty = fields.Integer(string="Quantity consumed")
remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func")
@api.onchange('qty', 'consumed_qty')
def _remaining_func(self):
if self.consumed_qty or self.qty:
self.remaining_qty = self.qty +(-self.consumed_qty)
因此,生产订单包含生产订单行,而打印订单也有订单行(One2many order_lines
字段)
两者都有一种方法,都称为_remaining_func_
。
对于remaining_qty
字段,这些都可以,但consumed_qty
应该在production.order和print.order之间相互关联。
因此,例如,如果qty
上的bsi.production.order.lines
为10,(还有其他方法可以从生产订单创建bsi.print.order
),并且bsi.print.order
我放了qty
在bsi.production.order.line
值为5时,原始10应该是_remaining_func_
上的5,我认为使用类似@login_required
def edit_sale_view(request, id):
instance = get_object_or_404(Sale, id=id)
form = EditSaleForm(instance=instance)
if request.method == "POST":
form = EditSaleForm(request.POST, instance=instance)
if form.is_valid():
obj = form.save(commit=False)
obj.user = request.user
obj.save()
return JsonResponse({'msg': 'Data saved'})
else:
return JsonResponse({'msg': 'Data not saved'})
return HttpResponse(form.as_p())
的方法我可以实现此目的,但我有点困惑如何在两个模型之间做到这一点。
有什么想法吗?
如果需要进一步说明,请告诉我。
答案 0 :(得分:2)
除非bsi.production.order
和bsi.print.order
之间的关系是1:1,否则无法管理您想要的内容,但在您的情况下,生产订单似乎可以有多个打印订单。我举个例子:
您可以在Many2one
中创建bsi.print.order.line
字段,指向bsi.production.order.line
:
class bsi_print_order_lines(models.Model):
_name = 'bsi.print.order.lines'
po_line_related = fields.Many2one('bsi.production.order.lines', ondelete='cascade', string="Production Order Line Related")
每次创建打印行时,您都可以轻松创建相关的生产线(您拥有所需的所有数据):
@api.model
def create(self, vals):
print_line = super(bsi_print_order_lines, self).create(vals)
po_line_vals = {
'production_order': print_line.print_order.production_orders.id,
'isbn': print_line.isbn,
'qty': print_line.qty,
'consumed_qty': print_line.consumed_qty,
'remaining_qty': print_line.remaining_qty,
}
po_line = self.env['bsi.production.order.lines'].create(po_line_vals)
return print_line
但是你必须以相反的方式做同样的事情(这次覆盖bsi.production.order.lines
ORM创建方法),在这里你会发现问题:
@api.model
def create(self, vals):
po_line = super(bsi_production_order_lines, self).create(vals)
print_line_vals = {
'production_orders': po_line.production_order.id,
'po_line_related': po_line.id,
'isbn': po_line.isbn,
'qty': po_line.qty,
'consumed_qty': po_line.consumed_qty,
'remaining_qty': po_line.remaining_qty,
'print_order': '???????' # You cannot know which print order you have to write here since a production order can have several ones...
}
print_line = self.env['bsi.print.order.lines'].create(print_line_vals)
return po_line
如果bsi.production.order
和bsi.print.order
之间的关系为1:1,则可以使用search
获取打印订单(因为您确定只会返回一条记录) :
@api.model
def create(self, vals):
po_line = super(bsi_production_order_lines, self).create(vals)
print_order = self.env['bsi.print.order'].search([
('production_orders', '=', po_line.production_order.id)
]).ensure_one()
print_line_vals = {
'production_orders': po_line.production_order.id,
'po_line_related': po_line.id,
'isbn': po_line.isbn,
'qty': po_line.qty,
'consumed_qty': po_line.consumed_qty,
'remaining_qty': po_line.remaining_qty,
'print_order': print_order.id,
}
print_line = self.env['bsi.print.order.lines'].create(print_line_vals)
return po_line
通过这种方式,您可以将生产线和打印行相关联,并且您还必须覆盖write
和unlink
方法以控制修改或删除行的时间,同样执行它的“双胞胎”(由于名为Many2one
的新po_line_related
字段,很容易找到它。
当然它不是一个漂亮的解决方案,但我认为它是唯一一个(使用Odoo API)实体关系图。