Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda