콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
4113 화면

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?

 

아바타
취소
베스트 답변

hello 

while you create new basket at that time the into create method you call update_sales method before the supercall. that's why the basket_id is not updated. because you get the newly created basket id into the supercall(in variable res). so call the method like res.update_sales(vals) then you get the value of self.id into line sale.write({'basket_id': self.id}) . 

make below change into your code.

@api.model
def create(self, vals):
    res = super(SubscriptionBasket, self).create(vals)
    res.update_sales(vals)
    return res
아바타
취소
관련 게시물 답글 화면 활동
2
4월 20
4435
1
11월 17
4677
0
4월 17
5027
0
11월 22
4415
2
9월 21
17277