Opened 19 months ago

Closed 19 months ago

Last modified 19 months ago

#34364 closed New feature (wontfix)

Add Today to ORM db.models.functions.datetime

Reported by: Matteo Vitali Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Could add a function in the ORM for handling today date that uses native templates of databases without having to go through some TruncDate(Now())

I'm assuming something like this:

class Today(Func):
    template = "CURRENT_DATE"
    output_field = DateField()

    def as_postgresql(self, compiler, connection, **extra_context):
        # PostgreSQL's CURRENT_TIMESTAMP means "the time at the start of the
        # transaction". Use STATEMENT_TIMESTAMP to be cross-compatible with
        # other databases.
        return self.as_sql(
            compiler, connection, template="STATEMENT_TIMESTAMP()", **extra_context
        )

    def as_mysql(self, compiler, connection, **extra_context):
        return self.as_sql(
            compiler, connection, template="CURRENT_DATE()", **extra_context
        )

    def as_sqlite(self, compiler, connection, **extra_context):
        return self.as_sql(
            compiler,
            connection,
            template="STRFTIME('%%%%Y-%%%%m-%%%%d', 'NOW')",
            **extra_context,
        )

What do you think about?

Change History (4)

comment:1 by Matteo Vitali, 19 months ago

Summary: Add Today to ORM db functions datetimeAdd Today to ORM db.models.functions.datetime

comment:2 by Matteo Vitali, 19 months ago

Version: 4.1dev

comment:3 by Mariusz Felisiak, 19 months ago

Resolution: wontfix
Status: newclosed

As far as I'm aware, after implementing #28643 we don't want to add more functions to the core unless they are common and supported by all backends. The current thread is to keep Django a core framework, not providing every utility which might be useful.

If you don't agree than first start a discussion on the DevelopersMailingList, where you'll reach a wider audience and see what other think, and ​follow triaging guidelines with regards to wontfix tickets.

comment:4 by Matteo Vitali, 19 months ago

I would consider my proposal as an improvement of an existing feature rather than a feature from scratch, but ok, I expected that proposals on changes to the ORM would undergo a more stringent filter, but no problem if the best channel for proposing any improvement is the mailing list I can try to open a discussion there, thanks.

Note: See TracTickets for help on using tickets.
Back to Top