2

I have a problem, it turns out that I want to verify the ID of my country that is "00000000-0" because it can have 7 or 8 numbers before the script and after the script it can end in 0,1,2,3,4,5,6 , 7,8,9, k. So, I have not managed to find a method to only allow me and accept that format. Could you please help me?

Example of DNI of my country:

18654675-6
19657346-k
1543564-0

I have this code

class instructor (models.Model):

     _name = 'gym.instructor'

     name = fields.Char (string = "Name", required = 'true')

     telefono = fields.Integer (string = "Telephone +56", size = 9, required = True)

     rut = fields.Char ()

     address = fields.Char (string = "Address", required = True)

     mail = fields.Char (string = "Email")

     class_id = fields.Many2one (comodel_name = 'gym.clase', string = 'Class', required = False)

     def digit_verifier (dni):

     pat = re.compile ('^ \ d {7,8} - [0-9k]')

     if pat.search (str (dni)) is not None:

        return True

     return False

record model = "ir.ui.view" id = "gym.instructor_list">

      <field name = "name"> instructor list </ field>

      <field name = "model"> gym.instructor </ field>

      <field name = "arch" type = "xml">

        <tree>

          <field name = "id" />

          <field name = "name" />

          <field name="rut"/>

          <field name = "telefono" />

          <field name = "address" />

          <field name = "mail" />

          <field name = "class_id" />

        </ tree>

      </ field>

    </ record>
cs95
  • 379,657
  • 97
  • 704
  • 746

2 Answers2

3

If you want something that looks like this:

import re

def digito_verificador(dni):
    pat = re.compile('^\d{7,8}-[0-9k]')

    if pat.search(str(dni)) is not None:
        return True

    return False

or

import re

def digito_verificador(dni):
    pat = re.compile('^\d{7,8}-[0-9k]')

    return pat.findall(dni)

Both of which will work for you, and if they don't elaborate!

Give the answer to Jacob Rodal, he came up with it first.

Oleg Vorobiov
  • 482
  • 5
  • 14
  • neither of the two codes works for me. I have to add @ api.one @ api.depends ("dni") before your code? – Francisco González Mejías Jul 06 '19 at 20:46
  • how? what error does it produce? you don't like how it works? how is it not working? ELABORATE! – Oleg Vorobiov Jul 06 '19 at 21:12
  • you do understand that we propose a way to find an ID in a string via `digito_verificador()` function and regex, right? how you use that function is on you! providing a string to search from is on you! and we can't really help you with the rest without the entirety of your code! – Oleg Vorobiov Jul 06 '19 at 21:27
  • what do you want from ID verification? and how did you screw up the regex so much?! i or anyone else can't help you unless YOU tell us what you want from your code, we can't read your mind! saying "add code to my question" doesn't really explain anything about your issue! – Oleg Vorobiov Jul 06 '19 at 21:51
  • basically I want that when entering data I accept 7 or 8 digits before the script and after the script can be added any of the following digits: 0.1.2.3.4.5.6.7.8.9.k and accept the structure xxxxxxxx-x – Francisco González Mejías Jul 06 '19 at 21:54
  • and why can't you do it yourself? we gave you the way to verify if a string is an ID or not, on the python side. how you re-implement it is not our problem! – Oleg Vorobiov Jul 06 '19 at 22:00
  • thank you very much, anything will keep you informed – Francisco González Mejías Jul 06 '19 at 22:03
2

I would use regex patterns. For example, you could try this pattern:

pattern = re.compile(r"^\d{7,8}-[0-9k]",re.M) The pattern will match any string that has 7 or 8 numbers followed by a dash followed by a single digit or a k. It will only match the start of a line or immediately after a newline. If this doesn't meet your use case, I would recommend researching regex expressions such that you can modify the pattern I've used.

It can be used like so:

import re

pattern = re.compile(r"^\d{7,8}-[0-9k]",re.M)
examples = """
matches:
18654675-6 
19657346-k
1543564-0
not matches:
random text
186546755-6 (too many numbers)
19657346-c (invalid ending)
153564-0 (too few numbers)
"""
valid_ids = pattern.findall(examples)
print(valid_ids)

Output: ['18654675-6', '19657346-k', '1543564-0']

Jacob Rodal
  • 620
  • 1
  • 6
  • 12