0

我刚刚将我的 Django 1.6.5 项目升级到 Django 1.7.1。我按照记录的步骤从 South 迁移到 Django 1.7.1,它运行良好。

我遇到的问题是当我在我的项目上运行“python manage.py test”时,它失败并出现错误:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/test/runner.py", line 147, in run_tests
    old_config = self.setup_databases()
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/test/runner.py", line 109, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/test/runner.py", line 299, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/db/backends/creation.py", line 377, in create_test_db
    test_flush=True,
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 115, in call_command
    return klass.execute(*args, **defaults)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 97, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 36, in database_forwards
    schema_editor.create_model(model)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 209, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 121, in column_sql
    default_value = self.effective_default(field)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 184, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/home/mark/.virtualenvs/clippy/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 627, in get_db_prep_save
    prepared=False)
  File "/home/mark/projects/clippy/core/fields.py", line 21, in get_db_prep_value
    return int(time.mktime(value.timetuple()))
AttributeError: 'NoneType' object has no attribute 'timetuple'

我相信这是由于我使用自定义字段将日期/时间字段存储为数据库中的时间戳整数。代码是:

class UnixDateTimeField(models.DateTimeField):
    __metaclass__ = models.SubfieldBase

    def get_internal_type(self):
        return 'PositiveIntegerField'

    def to_python(self, value):
        if value is None or isinstance(value, datetime):
            return value
        if isinstance(value, date):
            return datetime(value.year, value.month, value.day, 0, 0, 0, 0, utc)
        return datetime.utcfromtimestamp(float(value)).replace(tzinfo=utc)

    def get_db_prep_value(self, value, connection, prepared=False):
        return int(time.mktime(value.timetuple()))

    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return self.to_python(value).strftime('%Y-%m-%d %H:%M:%S %Z')

即使我从项目中删除所有单元测试也会发生这种情况。有谁知道是什么原因造成的?在升级到 Django 1.7.1 之前它运行良好。

4

2 回答 2

0

我在使用 Django 自定义字段时也遇到了一些错误,我认为问题在于 Django 的迁移系统。

尝试压缩/删除迁移(https://docs.djangoproject.com/en/1.7/topics/migrations/#squashing-migrations

于 2014-11-30T11:29:10.880 回答
0

我通过将以下内容添加到我的字段中的 get_db_prep_value() 方法解决了这个问题:

if value is None:
    return value

所以总场变成:

class UnixDateTimeField(models.DateTimeField):
    __metaclass__ = models.SubfieldBase

    def get_internal_type(self):
        return 'PositiveIntegerField'

    def to_python(self, value):
        if value is None or isinstance(value, datetime):
            return value
        if isinstance(value, date):
            return datetime(value.year, value.month, value.day, 0, 0, 0, 0, utc)
        return datetime.utcfromtimestamp(float(value)).replace(tzinfo=utc)

    def get_db_prep_value(self, value, connection, prepared=False):

        if value is None:
            return value

        return int(time.mktime(value.timetuple()))

    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return self.to_python(value).strftime('%Y-%m-%d %H:%M:%S %Z')

这解决了这个问题,现在测试工作。

于 2014-12-04T08:15:33.830 回答