Thanks for the tip, but besides defining the type of the output it is unclear how this can be used to perform a date difference operation.
What I would need is to convert *timezone.now()* into a date constant that can be understood by the database, whatever the database is. If I leave it as it is, I get the str() conversion, which is meaningless for the database. Even if I find some format *dd-mm-yyyy* that works with a given database, I wouldn't be sure that it works with other databases. I need the ORM to perform the conversion *Date Object -> Database date constant*. Then comes the date difference operation. Same thing: either the database understands the minus sign with date format, or it doesn't and need some DATEDIFF operator. Again I would need the ORM to explicitly set the right operator. My guess at the moment is that this cannot be achieved with Django ORM. I may apply the dict method and work with the dictionary instead, so that I can add custom fields more easily. Yoann Le dimanche 31 juillet 2016 09:25:58 UTC+2, Constantine Covtushenko a écrit : > > Hi Yoann, > > I am not sure exactly but believes you are on the right way. > Try improve your expression with ExpressionWrapper, as said here > <https://docs.djangoproject.com/en/1.9/ref/models/expressions/#using-f-with-annotations> > . > Probably the problem is that you use values of different types in your > expression. > > Regards > > On Sat, Jul 30, 2016 at 11:35 PM, Yoann Duriaud <[email protected] > <javascript:>> wrote: > >> Hello, >> I would like to annotate a query with the following expression: ([Due >> Date] - [Now])/[Interval]. [Due Date] and [Interval] are fields from the >> database, while [Now] should be "equal" to timezone.now(). >> >> So this would look like: >> .annotate(ratio=(F('due_date')-timezone.now())/F('Interval')) >> >> but this does not work. Does someone know how the expression should be >> written (if it is indeed feasible with Django ORM)? >> >> Thanks for your help! >> >> Yoann >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> To post to this group, send email to [email protected] >> <javascript:>. >> Visit this group at https://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/03dedd39-142f-468a-b6fd-b7ec551862e8%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-users/03dedd39-142f-468a-b6fd-b7ec551862e8%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/60119a06-5b1f-4e23-a142-627c65f14017%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

