Timezone import error

59 views
Skip to first unread message

ahmed.ab...@gmail.com

unread,
Nov 29, 2015, 10:17:29 AM11/29/15
to Django users
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

knbk

unread,
Nov 29, 2015, 11:23:43 AM11/29/15
to Django users
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

monoBOT

unread,
Nov 29, 2015, 11:38:41 AM11/29/15
to django...@googlegroups.com
Hello Ahmed

you are using the get method, get can only return 1 item and its returning more than 1 in your case 2, thats the second error, but there is an import error also somewhere in your code, but the traceback you are sending is not concluyent.

--
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...@googlegroups.com.
To post to this group, send email to django...@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/f6953f2e-f9a6-46c7-aec8-f337b772fc9b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
monoBOT
Visite mi sitio(Visit my site): monobotsoft.es/blog/

ahmed.ab...@gmail.com

unread,
Nov 30, 2015, 4:03:32 AM11/30/15
to Django users
Hi kbnk, monoBOT,

Thank you both, your answers on the spot, I have tried using the filter and it's working, I may need to delete one of the records to get this sorted, thanks again.

>>> Question.objects.filter(pub_date__year=current_year)
[<Question: What's up?>, <Question: What's up?>]

Best regards,
Ahmed Abdullah
Reply all
Reply to author
Forward
0 new messages