Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!