Proposal: Extend db.models.functions.datetime.Now

2023-02-26 Thread matteo vitali
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.


Re: Proposal: Extend db.models.functions.datetime.Now

2023-03-01 Thread matteo vitali
@ Adam,
thanks to you for your feedback

Il giorno martedì 28 febbraio 2023 alle 21:51:15 UTC+1 Adam Johnson ha 
scritto:

> Hi Matteo,
>
> I agree with Mariusz on the ticket - this is a step beyond what Django 
> should support. You've shown in your proposal that such a function takes 3 
> lines of code for PostgreSQL. I don't think this is a burden on developers 
> to implement when needed.
>
> Also, the TruncDate(Now()) pattern is easily wrapped up in a function:
>
> def Today():
> return TruncDate(Now())
>
> Thanks for the suggestion though.
>
> On Sun, Feb 26, 2023 at 7:13 AM matteo vitali  wrote:
>
>> 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-develop...@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
>>  
>> <https://groups.google.com/d/msgid/django-developers/f462dc8a-b53f-4256-b024-2555793ea0f1n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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/9c3f7df1-9df3-46d0-9af4-49663d04a3f6n%40googlegroups.com.