7

I want to remove required attribute from HTML Form. And it should give error from server side that this field is required. Previously i was using required self.fields['group_name'].required=False. But it is not giving error for blank or null data. Then i came to know use_required_attribute, but i don't know about it and how to use it.

class GroupForm(forms.ModelForm):
    use_required_attribute = False
    class Meta:
        model = Groups
        fields = ['group_name', 'group_description', 'group_status']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
1
  • What do you mean with "it is not giving error for blank or null data"? You can set up your own clean_group_name method in form definition. Regarding the required attribute, you can set it in the format definition as said in the documentation. Commented Feb 8, 2019 at 8:30

2 Answers 2

9

Use form = GroupForm(use_required_attribute=False) when you initialize your form in your views.py.

Sign up to request clarification or add additional context in comments.

Comments

0

In a ModelForm, you can set the required attribute to False upon form initialization:

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.fields['group_name'].required = False

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.