Sunday, November 29, 2015

Re: Timezone import error

Hi Ahmed,

When an error happens in an expect: block, python 3 shows the original error caught by the try/expect block, as well as the new error. In this case, the ImportError is the original error, and it's nothing you should worry about: Django catches it and handles it accordingly. Admittedly, the error is slightly confusing. The ImportError has nothing to do with timezones or pytz. 

The real error is the second one, the MultipleObjectsReturned exception. Question.objects.get() expects that exactly one object is returned by the database. If there are no objects returned, or more than one is returned, it will raise an expection. If you want to get all objects published in the current year, you can use Question.objects.filter(put_date__year=current_year) instead.

Marten

On Sunday, November 29, 2015 at 4:17:29 PM UTC+1, ahmed.ab...@gmail.com wrote:
Hi,

I'm practicing Django v 1.8.6 (Writing your first Django app, part 1) and getting error when trying to import timezone, also I have installed pytz, when I try to use the import timezone package I'm getting ImportError. 

My settings.py has USE_TZ set to true by default, also, I had to change the TIME_ZONE from UTC to Asia/Bahrain but still I'm getting the below error, kindly if anyone have advice on the below error?

>>> from polls.models import Question, Choice
>>> Question.objects.all()
[<Question: What's up?>, <Question: What's up?>]
>>> Question.objects.filter(id=1)
[<Question: What's up?>]
>>> Question.objects.filter(id=2)
[<Question: What's up?>]
>>> Question.objects.filter(id=3)
[]
>>> Question.objects.filter(question_text__startswith='What')
[<Question: What's up?>, <Question: What's up?>]
>>> from django.utils import timezone
>>> current_year = timezone.now().year
>>> Question.objects.get(pub_date__year=current_year)
Traceback (most recent call last):
  File "C:\Users\abdullaha\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\commands\shell.py", line 69, in h
andle
    self.run_shell(shell=options['interface'])
  File "C:\Users\abdullaha\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\commands\shell.py", line 61, in r
un_shell
    raise ImportError
ImportError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\abdullaha\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\manager.py", line 127, in manager_metho
d
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\abdullaha\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\query.py", line 338, in get
    (self.model._meta.object_name, num)
polls.models.MultipleObjectsReturned: get() returned more than one Question -- it returned 2!
>>>

Best regards,
Ahmed Abdullah

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/58d4cbd4-0cd4-46a1-b978-79d2544b7215%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment