Django models DateTimeField index on Date

Viewed 1530

Is it possible to index only the date aspect of a models.DateTimeField in Django?

I cant seem to find it in the documentation, and i'm not sure if doing something like this works

class Meta:
    indexes = [
        models.Index(fields=['created_at__year', 'created_at__month', 'created_at__day']),
    ]
1 Answers

This answer shows how to do it in Postgres. However, Django's indexes don't support this out of the box. You'll need to manage it yourself via migrations.RunSQL.

migrations.RunSQL(
    "CREATE INDEX foo_created_at_date_idx ON event ((created_at::DATE));",
    "DROP INDEX IF EXISTS foo_created_at_date_idx;",
)
Related