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

122
Vistas
Django optional choices

Currently, I have the following field in my model

event_duration = models.PositiveIntegerField(blank=False,
                                                 null=False,
                                                 choices=EVENT_DURATION_OPTIONAL_CHOICES,
                                                 validators=[
                                                     MaxValueValidator(270),
                                                     MinValueValidator(1)
                                                 ],
                                                 )

and in my forms.py I have

def __init__(self, *args, **kwargs):
    super(EventForm, self).__init__(*args, **kwargs)
    self.helper['event_duration'].wrap(DropDownTextInput)

and my DropDownTextInput:

class DropDownTextInput(Field):
    """
    Layout object for rendering dropdowntextinput::

        DropDownTextInput('field_name')
    """
    template = "widgets/dropDownTextFieldRendered.html"

    def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs):
        return super(DropDownTextInput, self).render(
            form, form_style, context, template_pack=template_pack,
            extra_context={'inline_class': 'inline'}
        )

I want it to allow people to select from the the choices I provide, but if I they really wanted to enter their own integer, they could also do so.

It currently renders exactly like I wanted (which is the same as in here), but if the user enters a value which is not in the choices list, the form becomes invalid and returns a message Select a valid choice. 99 is not one of the available choices.

Is there a way to set choices but make it optional (i.e. accepts other values outside the choices list)?

over 4 years ago · Santiago Trujillo
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