"In case you need a form field that only has one acceptable value, you can create a custom form field derived from forms.ChoiceField which only accepts one choice. Alternatively you can: Ignore the value of such a field entirely. Simply pass a single choice to a the forms.ChoiceField"
"from django import forms class FixedField(forms.ChoiceField): def __init__(self, *, value, **kwargs): kwargs["choices"] = [(value, value)] super().__init__(**kwargs) class SampleForm(forms.Form): expected = FixedField(value="predefined")"
A form field can be restricted to a single acceptable value by using a custom field subclass of forms.ChoiceField. The custom field sets the choices argument to a single tuple containing the desired value, ensuring only that value is accepted. An alternative is to ignore the field value entirely or supply a single choice to a standard forms.ChoiceField. Example usage shows a FixedField class that accepts a value parameter and a SampleForm that sets expected = FixedField(value="predefined").
Read at djangosnippets.org
Unable to calculate read time
Collection
[
|
...
]