This question has been flagged

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)

enter code here

  1. 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.

Avatar
Discard
Best Answer

Hi:

There seem to be a couple of syntax issues. You need to establish the linkage between the field and function for the computation to happen.

For example,

subscription_tier = fields.Char(string='Subscription Tier',readonly=True,compute='_compute_release_to_pay')
Also, you need to explicitly set the value of the field in the last line like so.
n_subscription.subscription_tier = result

You can read more about this here: 

https://www.odoo.com/documentation/13.0/reference/orm.html#computed-fields

EDIT:
Change the if...else to the following by removing the "any" and comparing 'gold' with the name. The comparison is case-sensitive, so you need to ensure that it matches your data.

                if n_subscription_line.product_id.name == 'gold':
result = gold'
break
else:
result = 'not'

Also the last line needs to be indented in by 4 spaces if you want it executed once for each subscription.



Avatar
Discard
Author

it works thank u I was stuck for hours

The customer must be one only subscription_tier how can I stop the section of multiple.

should I use onchange on the `n_subscription.recurring_invoice_line_ids` or something else

Author

sorry it works to add a value but it's always `not`

I have edited my previous reply and added more information related to your questions under the EDIT section

Author

Thank for your quick reply works like a charm and sorry for the delay i had uni finals