Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
3 Risposte
10779 Visualizzazioni

Odoo 12 CE

​​​Class Dia​gram: 

       ,-------------------.    
| question_request |
|-------------------|
| +Char Name |
| +Char LastName |
| +Integer Age |
| +Text Description |
|-------------------|
`-------------------'

Code

    from odoo import models, fields, api

class Request(models.Model):
_name = 'test.request'
_description = "Request"

name = fields.Char(string="Name", required=True)
last_name = fields.Char(string="Last Name", required=True)
age = fields.Integer(string="Age", required=True)
description = fields.Text()

Goal:

Feature for field's value manual validations by users.

Ex: Customer send a request with fiel's values as follow:​

    Name:     "Peterrrrrrrrrr"
LastName: "Smith"
Age:      150

It's not a feature about value validation ( odoo.api.constrains(*args) ), but about manual values verification by an user ("Peterrrrrr" is a valid value for the 'name' field, but an user need to confirm or verify that).

The first idea was to use an extra field for verification and another extra field for the comment

    Name:     State=Invalid, Comment="Typo error"
LastName: State=Valid
Age: State:Invalid, Comment="Confirm real age"

The first idea was to use an extra field for validation and another extra field for the comment

    from odoo import models, fields, api

class Request(models.Model):
_name = 'test.request'
_description = "Request"

name = fields.Char(string="Name", required=True)
last_name = fields.Char(string="Last Name", required=True)
age = fields.Integer(string="Age", required=True)
description = fields.Text()

# fields for validation
name_valid = fields.Selection([
('valid', 'Valid'),
('invalid', 'Invalid'),
('unvalidated', 'Unvalidated')
], default="unvalidated")
name_valid_comment = fields.Text()

But this is not a good aproach because the need of implmenting the 'field_value_validation' for each field and each model wich need the validation feature
So i thing to store the 'field_value_validation' in the 'FieldsValidation' related model as follows:

    from odoo import models, fields, api

class Request(models.Model):
_name = 'test.request'
_description = "Request"

name = fields.Char(string="Name", required=True)
last_name = fields.Char(string="Last Name", required=True)
age = fields.Integer(string="Age", required=True)
description = fields.Text()
fueld_validation_ids = ???

class FieldsValidation(models.Model):
_name = 'test.fields_validation'
_description = "Validation"

class_name = fields.Char(strng="Class")
record_id = ???
field_name = fields.Char(string="Field")
status = fields .Selection([
('valid', 'Valid'),
('invalid', 'Invalid'),
('unvalidated', 'Unvalidated')
], default="unvalidated")

Just right here i got stuck, so i thought to ask to the community.
Thanks in advance

Avatar
Abbandona
Autore Risposta migliore

Hi Hilar AK, thanks for your answer.

I'm not asking about validation ( odoo.api.constrains(*args) ), but about manual values verification by an user ("Peterrrr" is a valid value for the name field, but an user need to confirm or verify that). Maybe i don't explain it well. I'll update the question to clarify.

Avatar
Abbandona
Risposta migliore

I think Hilar's answer is the correct way to implement field validation.  

On top of that, in the newapi (v9 or newer), when raising exceptions, 'Warning' is deprecated, UserError and ValidationError are preferred in odoo api.  

UserError is more of a front-end way to avoid user input undesired value.   
ValidationError is designed for function or access right that type of thing.  

Also, I personally would make the constraint method private as well.

Avatar
Abbandona
Risposta migliore

Just add some constraints for the fields. You can use SQL Constraints or write in python also using the decorator 

@api.constraints. 

eg:


@api.constrains('field_name')
def field_name_constratints(self):
if condition:
raise Warning(message)


Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
4
ott 24
4541
0
nov 16
3367
1
ago 15
4146
0
mar 15
4495
1
dic 24
620