首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django AutoField错误

Django AutoField错误
EN

Stack Overflow用户
提问于 2015-03-26 21:02:05
回答 1查看 4K关注 0票数 2

我尝试将以下模式包含到我的django应用程序中,但总是得到一个断言错误。模式:

代码语言:javascript
复制
class Tuple(models.Model):
    fancyId = models.IntegerField()
    file1 = models.FileField()
    file2 = models.FileField()
    ccode1 = models.CharField(max_length=10000,default="123",unique=False)

我甚至尝试使用django文档中给出的相同示例,但最终得到了相同的错误!

抛出的错误如下:

代码语言:javascript
复制
  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.
EN

回答 1

Stack Overflow用户

发布于 2015-03-26 21:33:25

Django会自动生成主键id,所以Id是冗余的。参见the Django Book

除非您正在尝试将Django与遗留数据库集成。参见docs

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29279130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档