Hello,  after I updated django from 3.0.2 to 3.0.3 the result of the 
following code has changed when using a MySQL database. It produces a wrong 
sql query and also the wrong result for "*end_total_time"* attribute.
Is this a bug ? also is there any other way to get the wright result using 
3.0.3 ?

Thank you.

*models.py*
from datetime import datetime
import pytz


from django.db import models
from django.db.models.functions import Cast


class PhaseQueryset(models.QuerySet):


    def with_duration(self,):
        base_date = datetime(2000, 1, 3, 0, tzinfo=pytz.utc)


        # When I use base_date to do the end_total_time math in 3.0.3 
together
        # with ended_at annotated, it creates a wrong query
        qs = self.annotate(
            ended_at=models.Case(
                models.When(
                    models.Q(type='TYPEONE'),
                    then=models.functions.Now()
                ),
                default=models.F('started_at'),
                output_field=models.DateTimeField(),
            ),
            base_date=models.functions.Cast(
                models.Value(base_date),
                output_field=models.DateTimeField()
            ),
            end_total_time=models.ExpressionWrapper(
                models.F('ended_at') - models.F('base_date'),
                output_field=models.fields.BigIntegerField()
            )
        )


        return qs


# Create your models here.
class Phase(models.Model):
    objects = PhaseQueryset().as_manager()
    started_at = models.DateTimeField()
    type = models.CharField(max_length=40)



*tests.py*
from datetime import datetime, timedelta
import pytz


from django.test import TestCase
from daterror.models import Phase
# Create your tests here.


class TestDateProblem(TestCase):


    def setUp(self,):
        past = datetime.now(tz=pytz.UTC) - timedelta(days=30)
        Phase.objects.create(started_at=past, type='TYPEONE')
        past = datetime.now(tz=pytz.UTC) - timedelta(days=33)
        Phase.objects.create(started_at=past, type='TYPETWO')
        past = datetime.now(tz=pytz.UTC) - timedelta(days=34)
        Phase.objects.create(started_at=past, type='TYPETHREE')




    def test_timedifference_not_none(self,):
        phases = Phase.objects.all().with_duration()
        print(phases[0].end_total_time)
        print(phases[1].end_total_time)
        print(phases[2].end_total_time)
        self.assertNotEqual(None, phases[0].end_total_time)
        self.assertNotEqual(None, phases[1].end_total_time)
        self.assertNotEqual(None, phases[2].end_total_time)

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7b25cb7d-f213-486b-95b5-15d438277d01%40googlegroups.com.

Reply via email to