#24076: Query may fail with pytz exception
-------------------------------+----------------------------------------
     Reporter:  lvella         |                    Owner:  Akshat verma
         Type:  Bug            |                   Status:  assigned
    Component:  Documentation  |                  Version:  1.6
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Accepted
    Has patch:  1              |      Needs documentation:  1
  Needs tests:  1              |  Patch needs improvement:  1
Easy pickings:  1              |                    UI/UX:  1
-------------------------------+----------------------------------------
Changes (by Akshat verma):

 * cc: Akshat verma (added)
 * needs_better_patch:  0 => 1
 * needs_tests:  0 => 1
 * easy:  0 => 1
 * needs_docs:  0 => 1
 * has_patch:  0 => 1
 * ui_ux:  0 => 1


Old description:

> If I give date object as input to a filter on a DateTimeField field, like
> this:
>
> {{{
> class Ticket(models.Model):
>     register_time = models.DateTimeField(auto_now_add=True)
>
> day = datetime.strptime('2014-10-19', '%Y-%m-%d').date()
> Ticket.objects.filter(register_time__gte=day)
> }}}
>
> I may get a pytz.exceptions.NonExistentTimeError exception. The exact
> exception was:
>
> pytz.exceptions.NonExistentTimeError: 2014-10-19 00:00:00
>
> This date is the start of DST in my timezone:
>
> {{{
> TIME_ZONE = 'America/Sao_Paulo'
> }}}
>
> I believe this is a bug because Django tries to convert my input to a
> datetime object before building the query, and in this case, this
> conversion yields an invalid datetime.
>
> This bug seems very related to issue #9596, because in these cases,
> datetime should be converted to date, not the opposite.
>
> A minimal showcase is attached. Unzip and run ./manage.py test

New description:

 If I give date object as input to a filter on a DateTimeField field, like
 this:

 {{{
 class Ticket(models.Model):
     register_time = models.DateTimeField(auto_now_add=True)

 day = datetime.strptime('2014-10-19', '%Y-%m-%d').date()
 Ticket.objects.filter(register_time__gte=day)
 }}}

 I may get a pytz.exceptions.NonExistentTimeError exception. The exact
 exception was:

 pytz.exceptions.NonExistentTimeError: 2014-10-19 00:00:00

 This date is the start of DST in my timezone:

 {{{
 TIME_ZONE = 'America/Sao_Paulo'
 }}}

 I believe this is a bug because Django tries to convert my input to a
 datetime object before building the query, and in this case, this
 conversion yields an invalid datetime.

 This bug seems very related to issue #9596, because in these cases,
 datetime should be converted to date, not the opposite.

 A minimal showcase is attached. Unzip and run ./manage.py test


 # Changes by Akshat verma
 Traceback (most recent call last):
   File "./manage.py", line 10, in <module>
     execute_from_command_line(sys.argv)
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/core/management/__init__.py", line 385, in
 execute_from_command_line
     utility.execute()
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/core/management/__init__.py", line 354, in execute
     django.setup()
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/__init__.py", line 18, in setup
     from django.utils.log import configure_logging
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/utils/log.py", line 10, in <module>
     from django.views.debug import ExceptionReporter,
 get_exception_reporter_filter
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/views/debug.py", line 10, in <module>
     from django.http import (HttpResponse, HttpResponseServerError,
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/http/__init__.py", line 4, in <module>
     from django.http.response import (HttpResponse, StreamingHttpResponse,
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/http/response.py", line 13, in <module>
     from django.core.serializers.json import DjangoJSONEncoder
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/core/serializers/__init__.py", line 23, in <module>
     from django.core.serializers.base import SerializerDoesNotExist
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/core/serializers/base.py", line 6, in <module>
     from django.db import models
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/db/models/__init__.py", line 6, in <module>
     from django.db.models.query import Q, QuerySet, Prefetch  # NOQA
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/db/models/query.py", line 13, in <module>
     from django.db.models.fields import AutoField, Empty
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/db/models/fields/__init__.py", line 15, in <module>
     from django.db.models.lookups import default_lookups,
 RegisterLookupMixin
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/db/models/lookups.py", line 6, in <module>
     from django.utils import timezone
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/utils/timezone.py", line 149, in <module>
     utc = pytz.utc if pytz else UTC()
 AttributeError: 'module' object has no attribute 'utc'

 #Here is my model, which worked fine before installing pytz

 from django.db import models
 from django.conf import settings

 # Create your models here.
 class Item(models.Model):
     title = models.CharField(max_length=100, )
     description = models.TextField()
     seller = models.ForeignKey(settings.AUTH_USER_MODEL)
     price = models.DecimalField(max_digits=11, decimal_places=2)
     timestamp = models.DateTimeField(auto_now_add=True)
     last_updated = models.DateTimeField(auto_now=True)


     def __str__(self):
         return self.title

 #Edit: I added my views.py file

 from .models import Item

 from django.views.generic import ListView, DetailView

 class ItemListView(ListView):
     model = Item

 class ItemDetailView(DetailView):
     model = Item

     def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        #This is getting a dictionary of the objects being displayed to
        #the template
        context = super(ItemDetailView, self).get_context_data(**kwargs)
        # Add in a QuerySet of all items up for trade by the user
        context['other_things_for_trade_by_user'] =
 Item.objects.filter(seller=context['seller'])
        return context

--

Comment:

 Traceback (most recent call last):
   File "./manage.py", line 10, in <module>
     execute_from_command_line(sys.argv)
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/core/management/__init__.py", line 385, in
 execute_from_command_line
     utility.execute()
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/core/management/__init__.py", line 354, in execute
     django.setup()
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/__init__.py", line 18, in setup
     from django.utils.log import configure_logging
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/utils/log.py", line 10, in <module>
     from django.views.debug import ExceptionReporter,
 get_exception_reporter_filter
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/views/debug.py", line 10, in <module>
     from django.http import (HttpResponse, HttpResponseServerError,
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/http/__init__.py", line 4, in <module>
     from django.http.response import (HttpResponse, StreamingHttpResponse,
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/http/response.py", line 13, in <module>
     from django.core.serializers.json import DjangoJSONEncoder
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/core/serializers/__init__.py", line 23, in <module>
     from django.core.serializers.base import SerializerDoesNotExist
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/core/serializers/base.py", line 6, in <module>
     from django.db import models
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/db/models/__init__.py", line 6, in <module>
     from django.db.models.query import Q, QuerySet, Prefetch  # NOQA
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/db/models/query.py", line 13, in <module>
     from django.db.models.fields import AutoField, Empty
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/db/models/fields/__init__.py", line 15, in <module>
     from django.db.models.lookups import default_lookups,
 RegisterLookupMixin
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/db/models/lookups.py", line 6, in <module>
     from django.utils import timezone
   File "/Users/user/.virtualenvs/app/lib/python3.4/site-
 packages/django/utils/timezone.py", line 149, in <module>
     utc = pytz.utc if pytz else UTC()
 AttributeError: 'module' object has no attribute 'utc'

 #Here is my model, which worked fine before installing pytz

 from django.db import models
 from django.conf import settings

 # Create your models here.
 class Item(models.Model):
     title = models.CharField(max_length=100, )
     description = models.TextField()
     seller = models.ForeignKey(settings.AUTH_USER_MODEL)
     price = models.DecimalField(max_digits=11, decimal_places=2)
     timestamp = models.DateTimeField(auto_now_add=True)
     last_updated = models.DateTimeField(auto_now=True)


     def __str__(self):
         return self.title

 #Edit: I added my views.py file

 from .models import Item

 from django.views.generic import ListView, DetailView

 class ItemListView(ListView):
     model = Item

 class ItemDetailView(DetailView):
     model = Item

     def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        #This is getting a dictionary of the objects being displayed to
        #the template
        context = super(ItemDetailView, self).get_context_data(**kwargs)
        # Add in a QuerySet of all items up for trade by the user
        context['other_things_for_trade_by_user'] =
 Item.objects.filter(seller=context['seller'])
        return context

-- 
Ticket URL: <https://code.djangoproject.com/ticket/24076#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018746a2ce41-e2154b0d-8c15-423c-bc68-c99d1160e20e-000000%40eu-central-1.amazonses.com.

Reply via email to