Skip to Content
Menu
This question has been flagged
2 Replies
3156 Views

In the odoo environment it seems relatively simple to compute the priority. 

Once in developer mode, I simply create a new custom field with the dependencies and it works just fine.


<------------------------------->


field = x_priority : My selection field with the values [(low), (normal), (high), (higher), (highest)]


With the dependencies: [has_cc, arc, location] with either boolean or integer (location =0-4) values.


Now here my (working) code:


try:

    for record in self:

        if (record.has_cc) and (record.arc) and (record.location) >= "3":

record['x_priority'] = "4"

elif (record.has_cc) and (record.location) >= "3":

record['x_priority'] = "3"

elif (record.arc) and (record.location) >= "3":

record['x_priority'] = "3"

elif not (record.has_cc) and (record.location) >= "3":

record['x_priority'] = "2"

elif (record.location) == "2" or (record.location) == "1":

record['x_priority'] = "1"

else:

record['x_priority'] = "0"

except: None


<----------------------------------------------------->


well, so far so good.


Now here my Problem: 

How can I adapt this into working python code?

I tried the following in my model:


priority = fields.Selection(compute='_compute_priority', default='0', readonly=True)


@api.depends('has_cc', 'arc', 'location')

def _compute_priority(self):

try:

  for record in self:

    if (record.has_cc) and (record.arc) and (record.location) >= "3":

      record['priority'] = "4"

    elif (record.has_cc) and (record.location) >= "3":

      record['priority'] = "3"

    elif (record.arc) and (record.location) >= "3":

      record['priority'] = "3"

    elif not (record.has_cc) and (record.location) >= "3":

      record['priority'] = "2"

    elif (record.location) == "2" or (record.location) == "1":

      record['priority'] = "1"

    else:

      record['priority'] = "0"

except:None

This will result to stop (overload the server immidiately)

Anyone who can tell me what I am missing here?

Maybe there is also a complete different way to compute priority?

Avatar
Discard
Best Answer

It seems that you have not defined the selection values. Compute relates to computing the final value, while there no method to retrieve available values. E.g.:

priority = fields.Selection([("0", "low"), ("1", "middle"), ("2", "high")], compute='_compute_priority', default='0', readonly=True)
Avatar
Discard
Author Best Answer

Thanks for the quick answer:

here the full code:

priority = fields.Selection([('0', 'low'), ('1', 'middle'), ('2', 'high')], compute='_compute_priority',
default='0', readonly=True)

@api.depends('has_cc', 'arc', 'location')
def _compute_priority(self):
for record in self:
if record.has_cc and record.arc and record.location >= '3':
record['priority'] = '0',
elif record.has_cc and record.location >= '3':
record['priority'] = '1',
else:
record['priority'] = '2'

I still get an error:

  File "/opt/odoo/odoo/addons/yachtservice_app/models/yachtservice_checkin.py", line 44, in _compute_priority
    record['priority'] = '0',
  File "/opt/odoo/odoo/odoo/models.py", line 5058, in __setitem__
    return self._fields[key].__set__(self, value)
  File "/opt/odoo/odoo/odoo/fields.py", line 992, in __set__
    value = self.convert_to_cache(value, record)
  File "/opt/odoo/odoo/odoo/fields.py", line 1953, in convert_to_cache
    raise ValueError("Wrong value for %s: %r" % (self, value))
ValueError: Wrong value for yachtservice.checkin.priority: ('0',)

I tried single quotes (') and double quotes (") but have the same issue.
Avatar
Discard