djangojquery-ui-datepickerdjango-class-based-views

DatepickerWidget in CreateView


In my model I have a Datefield. So I want to use a Datepicker. How to use the Django-Admin Datepicker?

I have found examples to do this in a Form, but I have only desined a model. Is it possible to define this widget in my Model?


Solution

  • You can use get_form method to override widget attribute:

    class MyCreateView(CreateView):
        def get_form(self, form_class):
            form = super(MyCreateView, self).get_form(form_class)
            form.fields['date_field'].widget.attrs.update({'class': 'datepicker'})
            return form