Search code examples
pythonodooodoo-13

Odoo loop through a subscription line then set value to field


Hi am new to both python and Odoo development, I used the web interface for customization before. I was trying to create a class to

  1. add a field to sale.subscription Model

subscription_tier = fields.Char(string='Subscription Tier',readonly=True)

which is working but the second part is not working 2. loop through subscription line to see if the customer has silver or gold subscription then set it to the field subscription_tier

class subscription_tire_set(models.Model):
    _inherit = 'sale.subscription'

    subscription_tier = fields.Char(string='Subscription Tier',readonly=True)

    @api.depends('recurring_invoice_line_ids.product_id')
    def _compute_release_to_pay(self):
        for n_subscription in self:
            result = None
            for n_subscription_line in n_subscription.recurring_invoice_line_ids:
                if any(n_subscription_line.product_id) == 'gold':
                    
                    result = 'gold'
                    break
                else:
                    result = 'not'

        subscription_tier = result

I probably am doing something horribly wrong

also a getting this massge when trying to open any customer in subscription

Something went wrong! sale.subscription(10,).subscription_tier

Thank u for the help in advance.

What worked for me:

  result = None

  for n_subscription_line in n_subscription.recurring_invoice_line_ids:
                if n_subscription_line.product_id.name == 'gold'
                    result = 'gold'

                    # break

                else:
                    result

  n_subscription.subscription_tier = result 

Solution

  • It is not working because in the last line you are declaring a variable named subscription_tier and set its value to result, you should assign the result to subscription_tier field of the the n_subscription record:

    @api.depends('recurring_invoice_line_ids.product_id')
    def _compute_release_to_pay(self):
        for n_subscription in self:
            # Your code
            n_subscription.subscription_tier = result
    

    Edit:

    any will return True or False and your expression any(n_subscription_line.product_id) == 'gold' will always be evaluated to False