[Answered ]-Django daterangefield in a form

1👍

You can specify a different widget for a date field:

from django.forms.widgets import DateInput


class MyDateInput(DateInput):
    input_type = 'date'

next we can make a form DateField field for this:

from django.forms.fields import DateField


class MyDateField(DateField):
    widget = MyDateInput

and reuse that in a DateRangeField:

from django.contrib.postgres.forms.ranges import DateRangeField


class MyDateRangeField(DateRangeField):
    base_field = MyDateField

then we can use this as field:

class CreateCommissionElection(forms.ModelForm):
    electable_time = MyDateRangeField()

    class Meta:
        model = CommissionElection
        fields = [
            'electable_time',
        ]

Leave a comment