I am trying to create a timedelta dictionary based on different parameters. For eg.
from django.utils import timezone
a = {'Minutes':(lambda dt,delta: dt + timezone.timedelta(minutes=delta)),
'Hours': (lambda dt,delta: dt + timezone.timedelta(hours=delta)}
Now while using the above dictionary:
new_time = a['Minutes'](timezone.now(),10)
It gives following error:
/usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in <lambda>(dt, delta)
----> 1 a = {'Minutes':(lambda dt,delta: dt + timezone.timedelta(minutes=delta))}
NameError: global name 'timezone' is not defined
Please explain why I cannot use the imported function inside lambda.
ain the same module where you importeddjango.utils.timezone? Name lookups in the body of a lambda occur when the function is called, not defined../manage.py shell...it does something funny with the ipython context. This has been fixed in recent versions of Django. If I can find the reference I'll post as an answer. there was a bug ticket on Django about it