Best way to save date range in Django Rest Framework?

Hello everyone.

I’m working on a project where one of my models has to save a date range period, both of which are required fields. I have been needing to create these models for a past few years and always resorted to two DateFields.

Example:

class ExampleModel(models.Model):
    date_from = models.DateField()
    date_to = models.DateField()

This works fine, but you always have to work with two fields whenever doing something such as filtering, ordering, doing any sort of business logic.

Is there a better way to handle this type of data?

Within the database?

If you’re using PostgreSQL, you do have the DateRangeField (or DateTimeRangeField) available to you. That may satisfy your requirements for what you’re trying to do with those dates.

But, if you’re wanting to do things like filtering or sorting by just the start or the end date, you may find it easier to maintain them as separate fields.