[Django]-Django. How to set default option use generic CreateView

7👍

You can use initial like this:

class MyViewCreate(CreateView):
    model = MyModel
    form_class = MyFrom
    initial = {'status': '0'}
    template_name = 'mymodel_form.html'

or like this:

class MyViewCreate(CreateView):
    model = MyModel
    form_class = MyFrom
    template_name = 'mymodel_form.html'

    def get_initial(self):
        state = get_object_or_404(MyModel, some_field=some_value)
        return {
            'state':state,
        }

Hope it helps!

Leave a comment