Django: How to properly render SplitDateTimeWidget for DateTimeRangeField

Viewed 620

Online material (docs, code) for django's (postgres-specific) range fields is lacking. I tried overriding the widget for DateTimeRangeField, but can't get it to show 2 pretty date-time inputs using SplitDateTimeWidget (start datetime, end datetime) for my field. I have settled for overriding the type attribute and getting the following okayish output, without this, it's just plain type="text".

Current behaviour:

Current okayish behaviour

Expected behaviour: Following, but 2 of them.

enter image description here

My models.py

from django.db import models
from django.contrib.postgres.fields import DateTimeRangeField

class MyModel(models.Model):
    ...
    datetimerange = DateTimeRangeField()
    ...

My admin.py

from django import forms
from django.contrib import admin
from django.contrib.postgres.forms.ranges import RangeWidget, DateTimeRangeField


class MyModelAdminForm(forms.ModelForm):
    class Meta:
        model = models.MyModel
        fields = "__all__"
        widgets = {
            "datetimerange": RangeWidget(
                forms.SplitDateTimeWidget(
                    date_attrs={"type": "date"}, time_attrs={"type": "time"},
                ),
            )
        }


@admin.register(models.MyModel)
class MyModelAdmin(admin.ModelAdmin):
    form = MyModelAdminForm

I tried playing around with (inheriting from) the default field DateTimeRangeField but no luck there as well.

0 Answers
Related