Django开发bug及问题记录

开发环境

python:3.6.4
Django:1.11
IDE:pycharm
OS:windows10

自己新增的userprofile表去覆盖原有的auth_user表,遇到的错误:

1

Django1.11 在重写用户模型时报错:

AttributeError: type object ‘UserProfile’ has no attribute ‘USERNAME_FIELD’ models.py

新建用户模型UserProfile继承自AbstractBaseUser

debug时报错: AttributeError: type object 'UserProfile' has no attribute 'USERNAME_FIELD'

在模型中新增两行代码,即可解决

```identifier = models.CharField(max_length=40, unique=True)

USERNAME_FIELD = 'identifier'```

补充:事实证明, 网上的方法靠不住.用这个方式, 后面会在添加用户的时候,把username替换为identifier,创建用户提示username缺失.所以

identifier = models.CharField(max_length=40, unique=True)这一行删除

USERNAME_FIELD = 'identifier'这一行改成 USERNAME_FIELD = "username"

2

Django在执行python manage.py makemigrations的时候提示异常:

django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency user.0001_initial on database 'default'
原因:Django中有一个原生的User模型类,admin的模型依赖这个模型类,由于前面一个应用中的模型类User继承了AbstractUser类,所以提示这个错误。

解决方案:

删除数据库中 除了auth_user的其他表,然后重新来一次。直接删除数据库,再新建最快了,不然删除一堆的有外键的,会分成好几次执行.

参考

3

django中使用了自定义的UserProfile类,去覆盖django自己自带的auth_user类,发现不论怎么样,生成的userprofile表,总是只有前三个字段是继承auth_user表来的,后来才发现UserProfile类继承的不是AbstractUser类,而是继承了AbstractBaseUser类.前者是给用来继承所有auth_user类的的,后者是当auth_user中的字段,用户几乎都不要时才会去继承的.

4

django报1050, "Table 'django_content_type' already exists"
删除数据库,重新命名同名数据库.....

猜你喜欢

转载自www.cnblogs.com/xiujin/p/11284482.html
今日推荐