为什么我在makemigrations期间得到“AttributeError:'AutoField'对象没有属性'model'”

时间:2015-07-14 16:41:14

标签: python django database-migration

我正在尝试从Django 1.6.4升级到Django 1.8.3。

运行命令python manage.py makemigrations时,出现以下错误:

Migrations for '<app>':
  0001_initial.py:
    - Create model Foo
    - Create model bar
    ...
    - Add field rar
    - Add field naz
    ...
    - Alter unique_together for foo
<class 'django.db.migrations.writer.MigrationWriter'> User
<class 'django.db.migrations.writer.MigrationWriter'> (u'id', <django.db.models.fields.AutoField>)
<class 'django.db.migrations.writer.MigrationWriter'> id
<class 'django.db.migrations.writer.MigrationWriter'> Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/srv/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/srv/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/srv/venv/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/srv/venv/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/srv/venv/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 143, in handle
    self.write_migration_files(changes)
  File "/srv/venv/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 171, in write_migration_files
    migration_string = writer.as_string()
  File "/srv/venv/lib/python2.7/site-packages/django/db/migrations/writer.py", line 166, in as_string
    operation_string, operation_imports = OperationWriter(operation).serialize()
  File "/srv/venv/lib/python2.7/site-packages/django/db/migrations/writer.py", line 124, in serialize
    _write(arg_name, arg_value)
  File "/srv/venv/lib/python2.7/site-packages/django/db/migrations/writer.py", line 75, in _write
    arg_string, arg_imports = MigrationWriter.serialize(item)
  File "/srv/venv/lib/python2.7/site-packages/django/db/migrations/writer.py", line 304, in serialize
    item_string, item_imports = cls.serialize(item)
  File "/srv/venv/lib/python2.7/site-packages/django/db/migrations/writer.py", line 292, in serialize
    print cls, value
  File "/srv/venv/lib/python2.7/site-packages/django/utils/encoding.py", line 42, in <lambda>
    klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
  File "/srv/venv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 187, in __str__
    model = self.model
AttributeError: 'AutoField' object has no attribute 'model'

很明显<django.db.models.fields.AutoField>需要一个模型属性才能打印它而不会引发异常。我的问题有两个:

  1. Django为什么不将模型分配给AutoField类?
  2. 如何解决此错误,以便生成迁移?

1 个答案:

答案 0 :(得分:1)

我弄清楚为什么会出现这个错误。我在调试时错误地在Django源中留下了print语句(print cls, value)。

有关详细信息,请参阅上面回溯中的“django / db / migrations / writer.py”,第292行