Validation of fields

0

My question may be very simple, but how can I validate that in a numeric field they can only add positive numbers and with a specific extension (length). Sorry for the inconvenience, thanks

from django.db import models
from django.utils import timezone

class Personal(models.Model):

OPCIONES_GENERO_CHOICES = (
    ('Masculino', 'Masculino'),
    ('Femenino', 'Femenino'),
)
OPCIONES_ESTADO_CIVIL_CHOICES = (
    ('Casado(a)', 'Casado(a)'),
    ('Soltero(a)', 'Soltero(a)'),
    ('Viudo(a)', 'Viudo(a)')
)
OPCIONES_GRADO_INSTRUCCION_CHOICES = (
    ('Bachiller', 'Bachiller'),
    ('Universitaria', 'Universitaria'),
    ('Tecnico Superior', 'Tecnico superior'),
    ('Tecnico Medio', 'Tecnico Medio')
)
OPCIONES_CARGO_CHOICES = (
    ('Director(a)', 'Director(a)'),
    ('Analista', 'Analista'),
    ('Supervisor(a)', 'Supervisor(a)'),
    ('Empleado(a)','Empleado(a)'),
    ('Seguridad','Seguridad'),
    ('Supervisor seguridad', 'Supervisor de Seguridad'),
    ('Contratado(a)', 'Contratado(a)'),
    ('Obrero(a)', 'Obrero(a)')        
)

codigo_empleado = models.IntegerField(unique=True)
nombre = models.CharField(max_length=15)
apellidos = models.CharField(max_length=15)
ci = models.IntegerField(unique=True)
cargo = models.CharField(max_length=15, choices=OPCIONES_CARGO_CHOICES, blank=True, null=True)       
creado = models.DateTimeField(auto_now_add=True)
genero = models.CharField(max_length=12, choices=OPCIONES_GENERO_CHOICES, blank=True, null=True) 
email = models.EmailField()
telefono = models.CharField(max_length=12)        
direccion = models.CharField(max_length=200)
estado_civil = models.CharField(max_length=255, choices=OPCIONES_ESTADO_CIVIL_CHOICES, blank=True, null=True)
grado_instruccion = models.CharField(max_length=255, choices=OPCIONES_GRADO_INSTRUCCION_CHOICES, blank=True, null=True)
numero_de_hijos = models.IntegerField()
fecha_actualizacion = models.DateTimeField(auto_now=True)

def __str__(self):
    return '%s'% (self.nombre)

This is my model.py

python 3.5

    
asked by Jhonny Barreto 06.12.2018 в 16:52
source

2 answers

2

I'll assume you want to validate this field

numero_de_hijos = models.IntegerField(min_value=0, max_length=2, min_length=2)

With min_value is an attribute where the minimum value that can be entered is specified, when zeroing it will only accept positive numbers and with min and Max length the minimum and maximum size accepted by the input, since both attributes are the same value will only accept entering a two-digit number

    
answered by 06.12.2018 / 20:07
source
1

I suggest you read the Django documentation which is positiveintegerfield or positivesmallintegerfield

You can fix your model by putting one of these types.

    
answered by 06.12.2018 в 19:58