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'

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


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`的其他表,然后重新来一次。直接删除数据库,再新建最快了,不然删除一堆的有外键的,会分成好几次执行.

[参考](https://my.oschina.net/u/1446823/blog/861712)

 

###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"```
删除数据库,重新命名同名数据库.....
原文地址:https://www.cnblogs.com/xiujin/p/11284482.html