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)?