Django Forms

Arguments

views.py:

def get_form_kwargs(self):
    kwargs = super().get_form_kwargs()
    kwargs.update({"contact": self._contact()})
    return kwargs

forms.py:

class InvoiceUpdateForm(RequiredFieldForm):
    def __init__(self, *args, **kwargs):
        self.contact = kwargs.pop("contact")
        super().__init__(*args, **kwargs)

Fields

To change the text for a ChoiceField:

class UserModelMultipleChoiceField(forms.ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return "{} ({})".format(obj.get_full_name(), obj.username)

class AppSettingsForm(forms.ModelForm):
    class Meta:
        model = AppSettings
        fields = ("app_superusers", "contact_administrators")
        field_classes = {
            "app_superusers": UserModelMultipleChoiceField,
            "contact_administrators": UserModelMultipleChoiceField,
        }

Hidden

To hide a field:

if not settings.show_method:
    self.fields["methods"].widget = forms.HiddenInput()

Label

To change the label:

self.fields["category"].label = "Category"

Required

for name in ("assessor_comments", "result"):
   self.fields[name].required = True

Testing

assert HTTPStatus.FOUND == response.status_code, response.context["form"].errors