Hello, I am creating a model that will be linked to sales order, called "subscription.basket". This model's records (Baskets) have the fields shown bellow:
class SubscriptionBasket(models.Model):
_name="subscription.basket"
name = fields.Char("Nome", required=True)
date_start = fields.Date("Data Inicial", required=True)
date_end = fields.Date("Data Final", required=True)
sales_applied = fields.One2many(
string=u'Vendas Aplicadas',
comodel_name='sale.order',
inverse_name='basket_id',
)
On the 'sale.order' model, I added a field as following:
classSaleOrder(models.Model):
_inherit ="sale.order"
basket_id = fields.Many2one(
string=u'Cesta Aplicada',
comodel_name='subscription.basket',
And then, in order to link baskets to sales orders, I overwrote the create method as following:
@api.multi
def get_sales_in_range(self, date_start, date_end):
sales_in_range = self.env['sale.order'].search([('shippingEstimatedDate', '>=', date_start), ('shippingEstimatedDate','<=', date_end)])
return sales_in_range
@api.model
def create(self, vals):
self.update_sales(vals)
res = super(SubscriptionBasket, self).create(vals)
return res
@api.model
def update_sales(self, vals):
sales_in_range = self.get_sales_in_range(vals.get('date_start'), vals.get('date_start'))
for sale in sales_in_range:
sale.write({'basket_id': self.id})
What I expected is that when I created a "Basket A", all sales with the field shippingEstimatedDate within the range defined by the basket's date_start and date_end fields would have their basket_id field linked to "Basket A".
However, on creation nothing happens and no sales are updated. If try to duplicate "Basket A", creating "Basket B", the sales are then updated, getting linked to "Basket A". Is that expected? What should I do to update sales while creating a basket?