Django AutoField错误

时间:2015-03-26 13:02:05

标签: python django

我正在尝试将以下架构包含到我的django应用程序中,但总是会收到断言错误。 架构:

class Tuple(models.Model):
    fancyId = models.IntegerField()
    file1 = models.FileField()
    file2 = models.FileField()
    ccode1 = models.CharField(max_length=10000,default="123",unique=False)

我甚至尝试使用django文档中给出的相同示例,但结果却出现同样的错误!

抛出的错误是:

  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 108, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "C:\Python27\lib\site-packages\django\db\migrations\operations\fields.py", line 29, in database_forwards
    to_model = to_state.render().get_model(app_label, self.model_name)
  File "C:\Python27\lib\site-packages\django\db\migrations\state.py", line 67, in render
    model.render(self.apps)
  File "C:\Python27\lib\site-packages\django\db\migrations\state.py", line 316, in render
    body,
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 168, in __new__
    new_class.add_to_class(obj_name, obj)
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 297, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 919, in contribute_to_class
    "A model can't have more than one AutoField."
AssertionError: A model can't have more than one AutoField.

1 个答案:

答案 0 :(得分:4)

Django会自动生成主键id,因此Id是多余的。见the Django Book

除非您尝试将Django与旧数据库集成。见docs