简体   繁体   English

Django:创建自定义用户时,ValueError:对未知应用程序的依赖

[英]Django: when creating custom user, ValueError: Dependency on unknown app

I'm building a brand-new web project with Python 3.4.3 and Django 1.8 without any migrations made so far.我正在使用 Python 3.4.3 和 Django 1.8 构建一个全新的 Web 项目,到目前为止没有进行任何迁移。 For the project, I'm creating a custom user inheriting from AbstractBaseUser in an app called users .对于该项目,我正在一个名为users的应用程序中创建一个从AbstractBaseUser继承的自定义用户。 I've also correctly referred AUTH_USER_MODEL to the custom user in settings.py , before creating any migrations, as mentioned in the doc .文档中所述,在创建任何迁移之前,我还正确地将AUTH_USER_MODEL引用到settings.py中的自定义用户。

However, when I tried to run python manage.py makemigrations users or python manage.py migrate , the console reports ValueError: Dependency on unknown app: users .但是,当我尝试运行python manage.py makemigrations userspython manage.py migrate时,控制台报告ValueError: Dependency on unknown app: users

I'm pretty sure my code for the custom user model is correct because I followed documentation's example code, and also because, when I commented out AUTH_USER_MODEL , everything else worked fine except that Django created tables for the default User model, which is expected.我很确定我的自定义用户模型的代码是正确的,因为我遵循了文档的示例代码,还因为当我注释掉AUTH_USER_MODEL时,除了 Django 为默认用户模型创建了表之外,其他一切正常,这是预期的。

In the documentation it is mentioned that:文档中提到:

you must ensure that the model referenced by AUTH_USER_MODEL is created in the first migration of its app (usually called 0001_initial);您必须确保 AUTH_USER_MODEL 引用的模型是在其应用程序的第一次迁移中创建的(通常称为 0001_initial); otherwise, you will have dependency issues.否则,您将遇到依赖性问题。

I'm not very sure what exactly this means.我不太确定这到底是什么意思。 Does it mean that I have to manually create a migration for my users app?这是否意味着我必须为我的users应用手动创建迁移? If so, how exactly should I do it?如果是这样,我应该怎么做?

I greatly appreciate any suggestion or redirection to a credible source!我非常感谢任何建议或重定向到可靠来源! Thank you very much in advance!非常感谢您!

So after trying various approaches to tackle the problem, I found that the problem lie in an accidental mistake.所以在尝试了各种方法来解决这个问题后,我发现问题出在一个意外的错误上。 In the users directory, I've deleted all the files in migrations directory including its __init__.py .users目录中,我删除了migrations目录中的所有文件,包括其__init__.py If there is no __init__.py file, the ValueError will be thrown.如果没有__init__.py文件,则会抛出ValueError If you manually add the __init__.py file, everything works like a charm.如果您手动添加__init__.py文件,一切都会像魅力一样工作。

To those having the same problem, good luck!对于那些有同样问题的人,祝你好运!

For Django 3.2 deleting the migrations directory in the users directory and running对于 Django 3.2,删除users目录中的迁移目录并运行

python manage.py makemigrations users
python manage.py migrate

worked for me.为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM