Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
3891 Lượt xem

Hi, I created a custom module in Odoo, and added a few custom models that work successfully. But I've started having some issues when I tried to add a custom method to one of them.

I have this model named "azienda.py" (which, in English, would have been "company.py"), in which I've added this field.Char named "codice_ateco" (is a code that in Italy is given to each company to address its category, such as manufactoring, mining...). This code should have a structure of this kind: LETTER . NUMBER - NUMBER . NUMBER - NUMBER . NUMBER - NUMBER, but I didn't know how to give it this structure, so I just created a field.Char giving it size=7. 

class Azienda(models.Model):
  _name="piani_formazione.azienda"

[...] codice_ateco = fields.Char(string="Codice ATECO", size=7, required=True) [...]

From this point, I should create a new field.Selection called "categoria_rischio" ("risk_category") which could assume the valued "basso", "medio" or "alto" (in English, they would be "low", "medium" and "high") based on the value of the first letter of the "codice_ATECO". 

  [...] categoria_rischio = fields.Selection([('basso','Basso'),('medio','Medio'),
('alto','Alto'),('indefinite','error in codice ATECO definition')], compute="_compute_categoria_rischio", store=True,
string="Categoria di rischio")

And I defined the method as follows (at the end of the azienda.py file)

@api.depends("codice_ateco")
def _compute_categoria_rischio(self): 
  if ('B' or 'C' or 'D' or 'E' or 'F' or 'Q') in str(self.codice_ateco):
    self.categoria_rischio = 'alto'
  elif ('A' or 'H' or 'O' or 'P') in str(self.codice_ateco):
    self.categoria_rischio = 'medio'
  elif ('G' or 'I' or 'J' or 'K' or 'L' or 'M' or 'N' or 'R' or 'S' or 'T' or 'U') in str(self.codice_ateco):
    self.categoria_rischio = 'basso'
else:
self.categoria_rischio = 'indefinite'

Unfortunately, it works only for "A","B" and "G"; all the values after the ors give back the wrong value of the categoria_rischio, the 'error in codice ATECO definition' one (that I setted in the "else" option). 


EDITED: ups, I indented it wrong: I fixed it and modified the question according to the new issue: my question remain unresolved


EDITED 2: I used the or between the conditions and now the method works! 

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

 Try using "or" between conditions

if ('B') in str(self.codice_ateco) or ('C') in str(self.codice_ateco) etc...)

It's not the most elegant way to do it, but it usually does the job

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi, the method is inside the class ? Looks good...

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 2 25
3551
0
thg 5 24
46
1
thg 4 24
3353
4
thg 9 23
4836
2
thg 9 23
7050