[Fixed]-How to define initial values for ModelForm rightly?

1👍

If you don’t render your fields using {{ form.author }} and {{ form.news }} the form won’t validate. Try using a HiddenInput for each field, You can do that by overriding the __init__ method of your form:

class CommentForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(CommentForm, self).__init__(*args, **kwargs)
        self.fields['author'].widget = forms.HiddenInput()
        self.fields['news'].widget = forms.HiddenInput()

    class Meta:
        model = Comment
        fields = ['text','author','news']
👤César

Leave a comment