Hi everyone, 
it's my first thread here and sorry in advance for my bad english. 
Regarding the topic that I wanted to bring to the attention of the 
community, I have already opened a ticket and a thread on discord, but I 
was advised to try to open a discussion here to see if it might be of 
interest (I hope I won't go into too much length but I try to summarize my 
proposal as clearly and exhaustively as possible), so ... 

I already know that contributions on ORM changes or improvements don't have 
a good reputation for all the reasons I wholeheartedly agree with, but I 
noticed that in the db.models.functions.datetime section the date 
truncation could be done more natively using database templates. 
On the ticket that I had erroneously opened I drafted a snippet of proposal 
code that I link below 
https://code.djangoproject.com/ticket/34364?replyto=4#comment

My proposal is to extend the Now functionality of the ORM with a more 
native mode of the database. For example at the moment if we want to cast a 
datetime to date it is possible to do so by passing through a Truncate and 
then TrunDate(Now()) but the NOW functionality of the db (and I think it is 
supported by both postgresql and mysql) offers templates to format the date 
at your convenience. 

For example, if in one of our projects, where postgresql is used as a 
database, we wanted to avoid going through Truncate and take advantage of 
this native functionality of the database, we could quite simply extend Now 
with something like this: 

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

so we can compare a DateField with Today for example: 

MyModel.objects.filter(mydatefiled__lte=Today()) 

and not :

MyModel.objects.filter(mydatefiled__lte=TruncDate(Now()) 

Could it be useful to extend the Now function with native casts by 
leveraging database templates? I understand very well that extending the 
ORM also means maintaining all the parts of the code and backwards 
compatibility must be strongly guaranteed, and I share the approach of 
keeping it as minimal as possible. It is probably easier to override Now in 
your project when needed but I would like to hear your feedback on this.

--
Matteo Vitali
https://trottomv.me

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/f462dc8a-b53f-4256-b024-2555793ea0f1n%40googlegroups.com.
  • Pro... matteo vitali
    • ... 'Adam Johnson' via Django developers (Contributions to Django itself)
      • ... matteo vitali

Reply via email to