Ticket #374 (closed defect: fixed)
import leaks
| Reported by: | artem.skvira@… | Owned by: | andrew |
|---|---|---|---|
| Priority: | minor | Milestone: | 0.7 |
| Component: | migrations | Version: | 0.6.2 |
| Keywords: | import | Cc: |
Description
When importing datetime in a migration, like this:
from datetime import datetime
...
class Migration:
def forwards(self, orm):
d = datetime.now()
it gets replaced somehow with straight datetime module import, producing the following error when execution hits .now():
Running migrations for x:
- Migrating forwards to 0003_xxxxx.
x: 0002_xxxxx
Traceback (most recent call last):
File "./manage.py", line 14, in <module>
execute_manager(settings)
File "/usr/lib/pymodules/python2.6/django/core/management/init.py", line 362, in execute_manager
utility.execute()
File "/usr/lib/pymodules/python2.6/django/core/management/init.py", line 303, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/pymodules/python2.6/django/core/management/base.py", line 195, in run_from_argv
self.execute(*args, options.dict)
File "/usr/lib/pymodules/python2.6/django/core/management/base.py", line 222, in execute
output = self.handle(*args, options)
File "/home/user/Dropbox/x/external/south/management/commands/migrate.py", line 91, in handle
skip = skip,
File "/home/user/Dropbox/x/external/south/migration.py", line 581, in migrate_app
result = run_forwards(mapp, [mname], fake=fake, db_dry_run=db_dry_run, verbosity=verbosity)
File "/home/user/Dropbox/x/external/south/migration.py", line 388, in run_forwards
verbosity = verbosity,
File "/home/user/Dropbox/x/external/south/migration.py", line 329, in run_migrations
runfunc(orm)
File "/home/user/Dropbox/x/billing/migrations/0002_xxxxxxx.py", line 15, in forwards
start_time=datetime.now())
AttributeError?: 'module' object has no attribute 'now'

This is (unfortunately) a WONTFIX; the datetime module has to be overridden inside the migration to allow our generated field definitions to work (the repr() of dates/datetimes assumes you've imported the datetime module, not the datetime object).
Since this isn't a big blocker (you can always just use datetime.datetime, or move the import to inside the method definition), I'm not inclined to fix this, as it would break backwards compatability (it was arguably a bad decision at the time, but I'm stuck with it).