Django文档中的示例代码出错

时间:2016-04-26 04:17:04

标签: python django

我复制了完整的示例代码:[customizing admin user] [1](django文档的一部分),但是当我运行它时django不会出错,但是当我添加一个用户时,django会出现以下错误。 我没有更改示例的任何代码,并设置跟进文档。 跟进链接示例: https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#a-full-example

错误:

IntegrityError at /admin/app01/myuser/add/
NOT NULL constraint failed: app01_myuser.last_login
Request Method: POST
Request URL:    http://127.0.0.1:8000/admin/app01/myuser/add/
Django Version: 1.8.5
Exception Type: IntegrityError
Exception Value:    
NOT NULL constraint failed: app01_myuser.last_login
Exception Location: /Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py in execute, line       318

1 个答案:

答案 0 :(得分:0)

您在Django示例代码中使用的自定义用户模型与它在数据库中表示的表的定义之间明显存在差异。错误来自数据库,表示last_login表的app01_myuser列对其有NOT NULL约束,并且您正在尝试创建具有该字段的用户NULL。

您的MyUser模型包含从last_login继承的AbstractBaseUser字段。以下是last_login类中AbstractBaseUser的定义:

last_login = models.DateTimeField(_('last login'), blank=True, null=True)

根据last_login的定义,数据库中不应存在NOT NULL约束。

通过运行迁移确保您的数据库是最新的。观察运行迁移时发生的任何错误。

您还可以查看app01迁移代码,了解首先使用app01_myuser约束创建NOT NULL的时间和原因。

相关问题