2

Class Diagram:

   ,-------------------.    
   | 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 verification by users.

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

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

An employee user would be able to inform to the customer about the wrong values as follows:

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

It's not a feature about value validation (odoo.api.constrains(*args)), but about manual values verification by an user (abcde 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

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 verification
    name_verification = fields.Selection([
      ('valid', 'Valid'),
      ('invalid', 'Invalid'),
      ('notverified', 'Not verified')
    ], default="notverified")
    name_verification_comment = fields.Text()
    

​ But this is not a good approach because the need of implementing the 'field_value_verification' for each field and each model which need the verification feature. So I thought to store the 'field_value_verification' in the 'FieldsVerification' 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_verification_ids = ???
    
  class FieldsVerification(models.Model): 
    _name = 'test.fields_verification'
    _description = "Verification"
    
    class_name = fields.Char(strng="Class")
    record_id = ???
    field_name = fields.Char(string="Field")
    status = fields .Selection([
      ('valid', 'Valid'),
      ('invalid', 'Invalid'),
      ('notverified', 'Not verified')
    ], default="notverified")

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

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Nttcc
  • 41
  • 2

2 Answers2

0

Try out this code , it may help you.

from odoo.exceptions import UserError
from odoo import api, _

@api.constrains('field_name')
def field_name_constratints(self):

if your condition:
 raise UserError(_('Your Message!'))

For example , in the case of the field age

@api.constrains('age')
def age_constratints(self):

if self.age > 80 or self.age < 10:
   raise UserError(_('Confirm your real age !'))

Also don't forget to import the UserError and _

Ajmal JK
  • 813
  • 4
  • 14
  • Thanks for you answer. i'm not asking about validation but value verification by an user. Maybe don't explain it well. I'll update the question to clarify. – Nttcc Jun 10 '19 at 18:10
0

To do what you want, I'd suggest 2 possibilities.
Assume you have a form view displaying fields for user to input.

  1. Create a button triggers a wizard window.

  2. Another way is to override create() and write() method of the model to embed the wizard or any validation/condition formatting.

ORM wizard explaination

Example code for suggestion 1:
Below is model

    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()

        def call_wizard(self):
            view = self.env.ref('my_module.wizard_xml_id')
            wiz = self.env['verify.input'].create({})
            return {'name': _('Confirm Input?'),
                    'type': 'ir.actions.act_window',
                    'view_type': 'form',
                    'view_mode': 'form',
                    'res_model': 'verify.input',
                    'views': [(view.id, 'form')],
                    'view_id': view.id,
                    'target': 'new',
                    'res_id': wiz.id,
                    'context': {'default_name': self.name,
                                'default_last_name': self.last_name,
                                'default_age': self.age,
                                'default_description': self.description}
                    }

Below is wizard

    class Verify(models.TransientModel):  
        _name = 'verify.input'
        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()

Create an xml view for the wizard to display how you want it.

Cayprol
  • 21
  • 2