Skip to content

feat: add support for the field DateTimeRangeField from postgres.fields #950

@omargawdat

Description

@omargawdat

Suggestion:

Wouldn't it be nice to add support for the field DateTimeRangeField from Postgres as this field is used a lot.

Screenshot

image

Code

from django.contrib.postgres.constraints import ExclusionConstraint
from django.contrib.postgres.fields import DateTimeRangeField, RangeOperators
from django.db import models
from django.db.models import Q

class Event(models.Model):
    title = models.CharField(max_length=200)
    timespan = DateTimeRangeField(null=True)
    capacity = models.PositiveIntegerField()
    cancelled = models.BooleanField(default=False)

    class Meta:
        constraints = [
            ExclusionConstraint(
                name="prevent_overlapping_events",
                expressions=[
                    ("timespan", RangeOperators.OVERLAPS),
                ],
                condition=Q(cancelled=False),
            ),
            models.CheckConstraint(
                check=Q(capacity__gte=1),
                name="capacity_gte_1"
            ),
        ]

Documentation URL

https://docs.djangoproject.com/en/5.1/ref/contrib/postgres/fields/#daterangefield

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions