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

202
Vistas
Django - how to force user to select a certain amount of options in a checbox modelform?

I'm making a simple survey built around a modelform:

models.py

class Fruits(models.Model):
     author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
     title = models.CharField(max_length=200, default='')
     text = models.TextField(default='')
     banana = models.BooleanField(default=False)
     grape = models.BooleanField(default=False)
     apple = models.BooleanField(default=False)
     mango = models.BooleanField(default=False)

forms.py

class FruitPicker(ModelForm):
    class Meta:
        model = Fruits
        fields = [
        'title',
        'text',
        'banana',
        'grape',
        'apple',
        'mango'
    ]
    
    widgets = {
        'text': forms.Textarea(attrs={"style": "height:10em;" "width:60em;"}),
        'banana': forms.CheckboxInput(attrs={"style": "margin-left:350px;"}),
        'grape': forms.CheckboxInput(attrs={"style": "margin-left:350px;"}),
        'apple': forms.CheckboxInput(attrs={"style": "margin-left:350px;"}),
        'mango': forms.CheckboxInput(attrs={"style": "margin-left:350px;"})
    }

Now let's say I want to make sure the user has to select a minimum amount of 2 fruits, but a maximum amount of 3. How would I go about doing this on the back-end?

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

0

You can override the clean method of the form to check if between two and three BooleanFields are checked:

from django.core.exceptions import ValidationError

class FruitPicker(ModelForm):
     # ⋮

    def clean(self, *args, **kwargs):
        data = super().clean(*args, **kwargs)
        total = sum([data[x] for x in ['banana', 'grape', 'apple', 'mango']])
        if not (2 <= total <= 3):
            raise ValidationError('You should select between 2 and 3 fruits.')
        return data

Here the sum(…) sums up the booleans and uses 0 for False, and 1 for True, it thus counts the number of Trues. If that number is not between 2 and 3, then it raises a ValidationError.

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