Ok, I am ready.
I did exactly as I suggested and got *successful* result.
Please check screenshots below:
on admin:
[image: Inline image 1]
on table view with annotated field:
[image: Inline image 2]
were
-model
class Entity(models.Model):
due_date = models.DateTimeField(default=timezone.now())
interval = models.PositiveSmallIntegerField(default=2)
objects = EntityManager()
and manager
class EntityManager(models.Manager):
def get_items(self):
return self.all().annotate(
ratio=models.ExpressionWrapper(
(models.F('due_date') -
timezone.now())/models.F('interval'), models.DateTimeField()))
As we can see no dates format problem at all.
Django works as expected - perfectly!
Hope that helps.
Regards,
On Tue, Aug 2, 2016 at 8:35 AM, Constantine Covtushenko <
[email protected]> wrote:
> Hi,
>
> I am not cleat of what you suggested as a dictionary method and how you
> are going to use it.
>
> But any way let me check my guesses and I will be back.
>
> On Mon, Aug 1, 2016 at 11:25 PM, Yoann Duriaud <[email protected]>
> wrote:
>
>> 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]>
>>> 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].
>>>> 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/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
>> <https://groups.google.com/d/msgid/django-users/60119a06-5b1f-4e23-a142-627c65f14017%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/CAK52boVUbaHaEWYf_Y4pNbuosXqeiosGGAi5d3_ODRiCYHeAdg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.