Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
Override min_value and max_value in IntegerField Django
class A:
  min_v = 1
  max_v = 15
  number1 = IntegerField(min_value=min_v, max_value=max_v)

class B(A):
   number2 = IntegerField(min_value=A.min_v, max_value=A.max_v)

class C(B):
  min_v = 2
  max_v = 20

How to make both number1 and number2 to be in range from 2 to 20 in class C?

I'm thinking about two ways:

  1. In __init__ redeclare self.fields['number1'] like IntegerField(min_value=self.min_v)
  2. Set self.fields['number1'].min_value=self.min_v in __init__ and override setattr() to add validator.

What is the best and the cleanest way?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It might be better to add these bounds as validators to the the bounds in the __init__ method of class A and class B. This will make these transparent to C and furthermore C can for example define another IntegerField for that name and thus specify a different widget for that:

from django.core import validators

class A:
    number1 = IntegerField()
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        number1 = self.fields['number1']
        number1.validators.append(validators.MinValueValidator(self.min_v))
        number1.validators.append(validators.MaxValueValidator(self.max_v))
        number1.widget.attrs['min'] = number1.min_value = self.min_v
        number1.widget.attrs['max'] = number1.max_value = self.max_v

class B(A):
    number2 = IntegerField()
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        number2 = self.field['number2']
        number2.validators.append(validators.MinValueValidator(self.min_v))
        number2.validators.append(validators.MaxValueValidator(self.max_v))
        number2.widget.attrs['min'] = number2.min_value = self.min_v
        number1.widget.attrs['max'] = number2.max_value = self.max_v
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda