[Django]-The proper Django way to make a form field required on CreateView, but optional on UpdateView?

11👍

Here a solution to make a field required :

class Foo(CreateView):

    model = Fiz
    fields = ('bar',)

    def get_form(self, form_class=None):
        form = super(Foo, self).get_form(form_class)
        form.fields['bar'].required = True
        return form

0👍

i think no but you can implement in it !

class ModelXForm(ModelForm):

class Meta:
    model = x
    fields = ['image','name']

    def __init__(self,update, survey, **kwargs):
        super(FBRequestForm, self).__init__(**kwargs)
        if not update:
            self.fields['image'].widget.attrs['readonly'] = True
            self.fields['image'].widget = forms.HiddenInput()
👤mtt2p

Leave a comment