问题:Django模型“未声明显式的app_label”
我机智的结束了。经过十多个小时的故障排除(可能还有更多),我认为自己终于可以从事业务了,但是后来我得到了:
Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label
网络上对此信息很少,目前还没有解决方案解决了我的问题。任何建议将不胜感激。
我正在使用Python 3.4和Django 1.10。
在我的settings.py中:
INSTALLED_APPS = [
'DeleteNote.apps.DeletenoteConfig',
'LibrarySync.apps.LibrarysyncConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
我的apps.py文件如下所示:
from django.apps import AppConfig
class DeletenoteConfig(AppConfig):
name = 'DeleteNote'
和
from django.apps import AppConfig
class LibrarysyncConfig(AppConfig):
name = 'LibrarySync'
I’m at wit’s end. After a dozen hours of troubleshooting, probably more, I thought I was finally in business, but then I got:
Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label
There is SO LITTLE info on this on the web, and no solution out there has resolved my issue. Any advice would be tremendously appreciated.
I’m using Python 3.4 and Django 1.10.
From my settings.py:
INSTALLED_APPS = [
'DeleteNote.apps.DeletenoteConfig',
'LibrarySync.apps.LibrarysyncConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
And my apps.py files look like this:
from django.apps import AppConfig
class DeletenoteConfig(AppConfig):
name = 'DeleteNote'
and
from django.apps import AppConfig
class LibrarysyncConfig(AppConfig):
name = 'LibrarySync'
回答 0
您是否缺少将应用程序名称放入设置文件中的信息?这myAppNameConfig
是.manage.py createapp myAppName命令在apps.py生成的默认类。其中myAppName是您的应用程序的名称。
settings.py
INSTALLED_APPS = [
'myAppName.apps.myAppNameConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
这样,设置文件可以找出您要调用的应用程序。您可以通过在以下代码中添加以下代码来更改apps.py文件中的外观:
myAppName / apps.py
class myAppNameConfig(AppConfig):
name = 'myAppName'
verbose_name = 'A Much Better Name'
Are you missing putting in your application name into the settings file?
The myAppNameConfig
is the default class generated at apps.py by the .manage.py createapp myAppName command. Where myAppName is the name of your app.
settings.py
INSTALLED_APPS = [
'myAppName.apps.myAppNameConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
This way, the settings file finds out what you want to call your application. You can change how it looks later in the apps.py file by adding the following code in
myAppName/apps.py
class myAppNameConfig(AppConfig):
name = 'myAppName'
verbose_name = 'A Much Better Name'
回答 1
我收到相同的错误,但我不知道如何解决此问题。我花了很多时间才注意到,我有一个与django的manage.py相同的initcopy文件。
之前:
|-- myproject
|-- __init__.py
|-- manage.py
|-- myproject
|-- ...
|-- app1
|-- models.py
|-- app2
|-- models.py
后:
|-- myproject
|-- manage.py
|-- myproject
|-- ...
|-- app1
|-- models.py
|-- app2
|-- models.py
您会收到此“未声明显式app_label”错误,这非常令人困惑。但是删除此初始化文件解决了我的问题。
I get the same error and I don´t know how to figure out this problem. It took me many hours to notice that I have a init.py at the same direcory as the manage.py from django.
Before:
|-- myproject
|-- __init__.py
|-- manage.py
|-- myproject
|-- ...
|-- app1
|-- models.py
|-- app2
|-- models.py
After:
|-- myproject
|-- manage.py
|-- myproject
|-- ...
|-- app1
|-- models.py
|-- app2
|-- models.py
It is quite confused that you get this “doesn’t declare an explicit app_label” error. But deleting this init file solved my problem.
回答 2
使用PyCharm运行测试时,我遇到了完全相同的错误。我已经通过显式设置DJANGO_SETTINGS_MODULE
环境变量来修复它。如果您使用的是PyCharm,只需点击编辑配置按钮,然后选择环境变量。
将变量设置为your_project_name.settings
,这应该可以解决问题。
似乎发生此错误,因为PyCharm使用自己的运行测试manage.py
。
I had exactly the same error when running tests with PyCharm. I’ve fixed it by explicitly setting DJANGO_SETTINGS_MODULE
environment variable. If you’re using PyCharm, just hit Edit Configurations button and choose Environment Variables.
Set the variable to your_project_name.settings
and that should fix the thing.
It seems like this error occurs, because PyCharm runs tests with its own manage.py
.
回答 3
我在使用时得到了这个,./manage.py shell
然后我不小心从根项目级别目录中导入了
# don't do this
from project.someapp.someModule import something_using_a_model
# do this
from someapp.someModule import something_using_a_model
something_using_a_model()
I got this one when I used ./manage.py shell
then I accidentally imported from the root project level directory
# don't do this
from project.someapp.someModule import something_using_a_model
# do this
from someapp.someModule import something_using_a_model
something_using_a_model()
回答 4
作为使用Python3的菜鸟,我发现它可能是导入错误而不是Django错误
错误:
from someModule import someClass
对:
from .someModule import someClass
这种情况发生在几天前,但我真的无法复制它…我认为只有Django新手才可能遇到这种情况。
尝试在admin.py中注册模型:
from django.contrib import admin
from user import User
admin.site.register(User)
尝试运行服务器,错误看起来像这样
some lines...
File "/path/to/admin.py" ,line 6
tell you there is an import error
some lines...
Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label
更改user
为.user
,问题已解决
as a noob using Python3 ,I find it might be an import error instead of a Django error
wrong:
from someModule import someClass
right:
from .someModule import someClass
this happens a few days ago but I really can’t reproduce it…I think only people new to Django may encounter this.here’s what I remember:
try to register a model in admin.py:
from django.contrib import admin
from user import User
admin.site.register(User)
try to run server, error looks like this
some lines...
File "/path/to/admin.py" ,line 6
tell you there is an import error
some lines...
Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label
change user
to .user
,problem solved
回答 5
我刚才有同样的问题。我通过在应用程序名称上添加命名空间来修复我的问题。希望有人觉得这有帮助。
apps.py
from django.apps import AppConfig
class SalesClientConfig(AppConfig):
name = 'portal.sales_client'
verbose_name = 'Sales Client'
I had the same problem just now. I’ve fixed mine by adding a namespace on the app name. Hope someone find this helpful.
apps.py
from django.apps import AppConfig
class SalesClientConfig(AppConfig):
name = 'portal.sales_client'
verbose_name = 'Sales Client'
回答 6
我在测试中导入模型时遇到此错误,即鉴于此Django项目结构:
|-- myproject
|-- manage.py
|-- myproject
|-- myapp
|-- models.py # defines model: MyModel
|-- tests
|-- test_models.py
在test_models.py
我MyModel
以这种方式导入的文件中:
from models import MyModel
如果以这种方式导入,则问题已解决:
from myapp.models import MyModel
希望这可以帮助!
PS:也许这有点晚了,但是在我的代码中没有其他人找到如何解决此问题的答案,我想分享我的解决方案。
I got this error on importing models in tests, i.e. given this Django project structure:
|-- myproject
|-- manage.py
|-- myproject
|-- myapp
|-- models.py # defines model: MyModel
|-- tests
|-- test_models.py
in file test_models.py
I imported MyModel
in this way:
from models import MyModel
The problem was fixed if it is imported in this way:
from myapp.models import MyModel
Hope this helps!
PS: Maybe this is a bit late, but I not found in others answers how to solve this problem in my code and I want to share my solution.
回答 7
在继续遇到这个问题并继续回到这个问题之后,我想我会分享我的问题所在。
@Xeberdee正确的所有内容,请按照以下说明操作,看看是否可以解决问题,如果不是,这是我的问题:
在我的apps.py中,这就是我拥有的:
class AlgoExplainedConfig(AppConfig):
name = 'algo_explained'
verbose_name = "Explain_Algo"
....
我所做的就是在我的应用名称之前添加了项目名称,如下所示:
class AlgoExplainedConfig(AppConfig):
name = '**algorithms_explained**.algo_explained'
verbose_name = "Explain_Algo"
这样就解决了我的问题,之后我就可以运行makemigrations和migration命令!祝好运
After keep on running into this issue and keep on coming back to this question I thought I’d share what my problem was.
Everything that @Xeberdee is correct so follow that and see if that solves the issue, if not this was my issue:
In my apps.py this is what I had:
class AlgoExplainedConfig(AppConfig):
name = 'algo_explained'
verbose_name = "Explain_Algo"
....
And all I did was I added the project name in front of my app name like this:
class AlgoExplainedConfig(AppConfig):
name = '**algorithms_explained**.algo_explained'
verbose_name = "Explain_Algo"
and that solved my problem and I was able to run the makemigrations and migrate command after that! good luck
回答 8
我今天在尝试运行Django测试时遇到此错误,因为我from .models import *
在其中一个文件中使用了速记语法。问题是我的文件结构如下:
apps/
myapp/
models/
__init__.py
foo.py
bar.py
在models/__init__.py
我使用速记语法导入模型时:
from .foo import *
from .bar import *
在我的应用程序中,我正在导入如下模型:
from myapp.models import Foo, Bar
这导致了Django model doesn't declare an explicit app_label
运行时./manage.py test
。
要解决此问题,我必须从完整路径中显式导入models/__init__.py
:
from myapp.models.foo import *
from myapp.models.bar import *
那解决了错误。
H / t https://medium.com/@michal.bock/fix-weird-exceptions-when-running-django-tests-f58def71b59a
I had this error today trying to run Django tests because I was using the shorthand from .models import *
syntax in one of my files. The issue was that I had a file structure like so:
apps/
myapp/
models/
__init__.py
foo.py
bar.py
and in models/__init__.py
I was importing my models using the shorthand syntax:
from .foo import *
from .bar import *
In my application I was importing models like so:
from myapp.models import Foo, Bar
This caused the Django model doesn't declare an explicit app_label
when running ./manage.py test
.
To fix the problem, I had to explicitly import from the full path in models/__init__.py
:
from myapp.models.foo import *
from myapp.models.bar import *
That took care of the error.
H/t https://medium.com/@michal.bock/fix-weird-exceptions-when-running-django-tests-f58def71b59a
回答 9
就我而言,这是因为我在项目级urls.py中使用了相对模块路径,INSTALLED_APPS
而apps.py
不是植根于项目根目录中。即,绝对模块路径始终存在,而不是相对模块路径+ hack。
无论我在应用程序中INSTALLED_APPS
和apps.py
应用程序中弄乱了多少,我都无法同时使用它们runserver
,pytest
直到这三个都植根于项目根目录为止。
资料夹结构:
|-- manage.py
|-- config
|-- settings.py
|-- urls.py
|-- biz_portal
|-- apps
|-- portal
|-- models.py
|-- urls.py
|-- views.py
|-- apps.py
用下面的,我可以运行manage.py runserver
,并与WSGI和使用gunicorn portal
应用看法,并没有麻烦,但pytest将与错误ModuleNotFoundError: No module named 'apps'
,尽管DJANGO_SETTINGS_MODULE
被正确配置。
config / settings.py:
INSTALLED_APPS = [
...
"apps.portal.apps.PortalConfig",
]
biz_portal / apps / portal / apps.py:
class PortalConfig(AppConfig):
name = 'apps.portal'
config / urls.py:
urlpatterns = [
path('', include('apps.portal.urls')),
...
]
改变应用程序的参考配置/ settings.py来biz_portal.apps.portal.apps.PortalConfig
和PortalConfig.name
到biz_portal.apps.portal
允许pytest来运行(我没有做检查portal
的意见还),但runserver
将与错误
RuntimeError:模型类apps.portal.models.Business没有声明显式的app_label,并且不在INSTALLED_APPS中的应用程序中
最后,我摸索apps.portal
着查看仍然在使用相对路径的内容,并发现config / urls.py也应该使用biz_portal.apps.portal.urls
。
In my case, this was happening because I used a relative module path in project-level urls.py, INSTALLED_APPS
and apps.py
instead of being rooted in the project root. i.e. absolute module paths throughout, rather than relative modules paths + hacks.
No matter how much I messed with the paths in INSTALLED_APPS
and apps.py
in my app, I couldn’t get both runserver
and pytest
to work til all three of those were rooted in the project root.
Folder structure:
|-- manage.py
|-- config
|-- settings.py
|-- urls.py
|-- biz_portal
|-- apps
|-- portal
|-- models.py
|-- urls.py
|-- views.py
|-- apps.py
With the following, I could run manage.py runserver
and gunicorn with wsgi and use portal
app views without trouble, but pytest would error with ModuleNotFoundError: No module named 'apps'
despite DJANGO_SETTINGS_MODULE
being configured correctly.
config/settings.py:
INSTALLED_APPS = [
...
"apps.portal.apps.PortalConfig",
]
biz_portal/apps/portal/apps.py:
class PortalConfig(AppConfig):
name = 'apps.portal'
config/urls.py:
urlpatterns = [
path('', include('apps.portal.urls')),
...
]
Changing the app reference in config/settings.py to biz_portal.apps.portal.apps.PortalConfig
and PortalConfig.name
to biz_portal.apps.portal
allowed pytest to run (I don’t have tests for portal
views yet) but runserver
would error with
RuntimeError: Model class apps.portal.models.Business doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS
Finally I grepped for apps.portal
to see what’s still using a relative path, and found that config/urls.py should also use biz_portal.apps.portal.urls
.
回答 10
当我尝试为单个应用生成迁移时遇到了此错误,该应用由于git合并而存在现有的格式错误的迁移。例如
manage.py makemigrations myapp
当我删除它的迁移然后运行:
manage.py makemigrations
不会发生该错误,并且迁移成功生成。
I ran into this error when I tried generating migrations for a single app which had existing malformed migrations due to a git merge. e.g.
manage.py makemigrations myapp
When I deleted it’s migrations and then ran:
manage.py makemigrations
the error did not occur and the migrations generated successfully.
回答 11
我有一个类似的问题,但是我可以通过在模型类中使用Meta类明确指定app_label来解决我的问题
class Meta:
app_label = 'name_of_my_app'
I had a similar issue, but I was able to solve mine by specifying explicitly the app_label using Meta Class in my models class
class Meta:
app_label = 'name_of_my_app'
回答 12
I got this error while trying to upgrade my Django Rest Framework app to DRF 3.6.3 and Django 1.11.1.
For anyone else in this situation, I found my solution in a GitHub issue, which was to unset the UNAUTHENTICATED_USER
setting in the DRF settings:
# webapp/settings.py
...
REST_FRAMEWORK = {
...
'UNAUTHENTICATED_USER': None
...
}
回答 13
我只是遇到了这个问题,并弄清楚出了什么问题。由于以前没有答案描述发生在我身上的问题,因此我将其发布给其他人:
- 问题出
python migrate.py startapp myApp
在我的项目根文件夹中,然后将myApp移到一个子文件夹中mv myApp myFolderWithApps/
。
- 我写了myApp.models并运行
python migrate.py makemigrations
。一切顺利。
- 然后我对另一个从myApp导入模型的应用做了同样的操作。b!执行迁移时,我遇到了这个错误。那是因为我不得不使用
myFolderWithApps.myApp
引用我的应用程序,但是我却忘记了更新MyApp / apps.py。因此,我在第二个应用程序中更正了myApp / apps.py,设置/ INSTALLED_APPS和导入路径。
- 但是随后错误不断发生:原因是我进行了迁移,试图使用错误的路径从myApp导入模型。我试图更正迁移文件,但我发现重置数据库和删除迁移从头开始更容易。
简而言之:-问题最初是由于myApp的apps.py,第二个应用程序的设置和导入路径中错误的应用程序名称引起的。-但这不足以更正这三个位置的路径,因为迁移是使用导入引用了错误的应用程序名称创建的。因此,相同的错误在迁移过程中始终发生(这次迁移除外)。
所以…请检查您的迁移情况,祝您好运!
I just ran into this issue and figured out what was going wrong. Since no previous answer described the issue as it happened to me, I though I would post it for others:
- the issue came from using
python migrate.py startapp myApp
from my project root folder, then move myApp to a child folder with mv myApp myFolderWithApps/
.
- I wrote myApp.models and ran
python migrate.py makemigrations
. All went well.
- then I did the same with another app that was importing models from myApp. Kaboom! I ran into this error, while performing makemigrations. That was because I had to use
myFolderWithApps.myApp
to reference my app, but I had forgotten to update MyApp/apps.py. So I corrected myApp/apps.py, settings/INSTALLED_APPS and my import path in my second app.
- but then the error kept happening: the reason was that I had migrations trying to import the models from myApp with the wrong path. I tried to correct the migration file, but I went at the point where it was easier to reset the DB and delete the migrations to start from scratch.
So to make a long story short:
– the issue was initially coming from the wrong app name in apps.py of myApp, in settings and in the import path of my second app.
– but it was not enough to correct the paths in these three places, as migrations had been created with imports referencing the wrong app name. Therefore, the same error kept happening while migrating (except this time from migrations).
So… check your migrations, and good luck!
回答 14
在Django rest_framework中构建API时遇到类似的错误。
RuntimeError:模型类apps.core.models.University未声明显式> app_label,并且不在INSTALLED_APPS中的应用程序中。
luke_aus的答案通过纠正我的问题帮助了我 urls.py
从
from project.apps.views import SurgeryView
至
from apps.views import SurgeryView
I’ve got a similar error while building an API in Django rest_framework.
RuntimeError: Model class apps.core.models.University doesn’t declare an explicit > app_label and isn’t in an application in INSTALLED_APPS.
luke_aus’s answer helped me by correcting my urls.py
from
from project.apps.views import SurgeryView
to
from apps.views import SurgeryView
回答 15
就我而言,将代码从Django 1.11.11移植到Django 2.2时出现此错误。我正在定义一个自定义FileSystemStorage派生类。在Django 1.11.11中,我在models.py中包含以下行:
from django.core.files.storage import Storage, DefaultStorage
后来在文件中,我有了类定义:
class MyFileStorage(FileSystemStorage):
但是,在Django 2.2中,我需要FileSystemStorage
在导入时显式引用类:
from django.core.files.storage import Storage, DefaultStorage, FileSystemStorage
和贴吧,错误消失了。
请注意,每个人都在报告Django服务器随地吐痰的错误消息的最后一部分。但是,如果向上滚动,则会在该错误的中间部分找到原因。
In my case I got this error when porting code from Django 1.11.11 to Django 2.2. I was defining a custom FileSystemStorage derived class. In Django 1.11.11 I was having the following line in models.py:
from django.core.files.storage import Storage, DefaultStorage
and later in the file I had the class definition:
class MyFileStorage(FileSystemStorage):
However, in Django 2.2 I need to explicitly reference FileSystemStorage
class when importing:
from django.core.files.storage import Storage, DefaultStorage, FileSystemStorage
and voilà!, the error dissapears.
Note, that everyone is reporting the last part of the error message spitted by Django server. However, if you scroll up you will find the reason in the middle of that error mambo-jambo.
回答 16
就我而言,我能够找到一个修复程序,并且通过查看其他人的代码也可能是相同的问题。文件。
希望这对某人有帮助。这是我对编码社区的第一个贡献
in my case I was able to find a fix and by looking at the everyone else’s code it may be the same issue.. I simply just had to add ‘django.contrib.sites’ to the list of installed apps in the settings.py file.
hope this helps someone. this is my first contribution to the coding community
回答 17
TL; DR:添加空白__init__.py为我解决了此问题。
我在PyCharm中遇到此错误,并意识到我的设置文件根本没有被导入。没有明显的错误告诉我,但是当我在settings.py中放入一些废话时,它没有引起错误。
我在local_settings文件夹中有settings.py。但是,我希望在同一文件夹中包含__init__.py以便导入。一旦我添加了这个,错误就消失了。
TL;DR: Adding a blank __init__.py fixed the issue for me.
I got this error in PyCharm and realised that my settings file was not being imported at all. There was no obvious error telling me this, but when I put some nonsense code into the settings.py, it didn’t cause an error.
I had settings.py inside a local_settings folder. However, I’d fogotten to include a __init__.py in the same folder to allow it to be imported. Once I’d added this, the error went away.
回答 18
如果所有配置正确,则可能只是导入混乱。密切关注您如何导入违规模型。
以下无效from .models import Business
。请使用完整的导入路径:from myapp.models import Business
If you have got all the config right, it might just be an import mess. keep an eye on how you are importing the offending model.
The following won’t work from .models import Business
. Use full import path instead: from myapp.models import Business
回答 19
如果所有其他方法均失败,并且在尝试导入PyCharm“ Python控制台”(或“ Django控制台”)时遇到此错误:
尝试重新启动控制台。
这真是令人尴尬,但是花了我一段时间我才意识到自己忘记这样做了。
这是发生了什么:
添加了一个新的应用程序,然后添加了一个最小模型,然后尝试在Python / Django控制台中导入模型(PyCharm pro 2019.2)。这引发了doesn't declare an explicit app_label
错误,因为我没有将新应用添加到中INSTALLED_APPS
。因此,我将应用添加到了INSTALLED_APPS
,再次尝试了导入,但是仍然遇到相同的错误。
来到这里,阅读所有其他答案,但似乎没有合适的答案。
最后,令我惊讶的是,在将新应用添加到后,我还没有重新启动Python控制台INSTALLED_APPS
。
注意:在将新对象添加到模块后,无法重新启动PyCharm Python控制台也是一种使人非常困惑的好方法 ImportError: Cannot import name ...
If all else fails, and if you are seeing this error while trying to import in a PyCharm “Python console” (or “Django console”):
Try restarting the console.
This is pretty embarassing, but it took me a while before I realized I had forgotten to do that.
Here’s what happened:
Added a fresh app, then added a minimal model, then tried to import the model in the Python/Django console (PyCharm pro 2019.2). This raised the doesn't declare an explicit app_label
error, because I had not added the new app to INSTALLED_APPS
.
So, I added the app to INSTALLED_APPS
, tried the import again, but still got the same error.
Came here, read all the other answers, but nothing seemed to fit.
Finally it hit me that I had not yet restarted the Python console after adding the new app to INSTALLED_APPS
.
Note: failing to restart the PyCharm Python console, after adding a new object to a module, is also a great way to get a very confusing ImportError: Cannot import name ...
回答 20
O … M … G我也遇到了这个错误,我花了将近2天的时间,现在终于设法解决了。老实说…错误与问题无关。就我而言,这只是语法问题。我试图独立运行一个在django上下文中使用某些django模型的python模块,但该模块本身不是django模型。但是我宣布全班不对
而不是
class Scrapper:
name = ""
main_link= ""
...
我在做
class Scrapper(Website):
name = ""
main_link= ""
...
这显然是错误的。该消息是如此令人误解,以至于我忍不住想,但我认为这是配置方面的问题,或者只是以错误的方式使用django,因为我对此很陌生。
我将在这里与新手分享,因为我经历同样的愚蠢期盼能解决他们的问题。
O…M…G
I was getting this error too and I spent almost 2 days on it and now I finally managed to solve it. Honestly…the error had nothing to do with what the problem was.
In my case it was a simple matter of syntax. I was trying to run a python module standalone that used some django models in a django context, but the module itself wasn’t a django model. But I was declaring the class wrong
instead of having
class Scrapper:
name = ""
main_link= ""
...
I was doing
class Scrapper(Website):
name = ""
main_link= ""
...
which is obviously wrong. The message is so misleading that I couldn’t help myself but think it was some issue with configuration or just using django in a wrong way since I’m very new to it.
I’ll share this here for someone newbie as me going through the same silliness can hopefully solve their issue.
回答 21
我SECRET_KEY
从环境变量中移出并在运行应用程序时忘记对其进行设置后,收到了此错误。如果你有这样的事情settings.py
SECRET_KEY = os.getenv('SECRET_KEY')
然后确保您实际上在设置环境变量。
I received this error after I moved the SECRET_KEY
to pull from an environment variable and forgot to set it when running the application. If you have something like this in your settings.py
SECRET_KEY = os.getenv('SECRET_KEY')
then make sure you are actually setting the environment variable.
回答 22
很可能您有依赖进口。
在我的情况下,我在模型中使用了序列化程序类作为参数,而序列化程序类则在使用以下模型:serializer_class = AccountSerializer
from ..api.serializers import AccountSerializer
class Account(AbstractBaseUser):
serializer_class = AccountSerializer
...
并在“序列化器”文件中:
from ..models import Account
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = (
'id', 'email', 'date_created', 'date_modified',
'firstname', 'lastname', 'password', 'confirm_password')
...
Most probably you have dependent imports.
In my case I used a serializer class as a parameter in my model, and the serializer class was using this model:
serializer_class = AccountSerializer
from ..api.serializers import AccountSerializer
class Account(AbstractBaseUser):
serializer_class = AccountSerializer
...
And in the “serializers” file:
from ..models import Account
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = (
'id', 'email', 'date_created', 'date_modified',
'firstname', 'lastname', 'password', 'confirm_password')
...
回答 23
我今天遇到了这个错误,并在谷歌搜索后最终到达了这里。现有的答案似乎都与我的情况无关。我唯一需要做的就是从__init__.py
应用程序顶层的文件中导入模型。我必须使用模型将导入移动到函数中。
Django似乎有一些奇怪的代码,在许多不同的情况下都可能会失败!
I got this error today and ended up here after googling. None of the existing answers seem relevant to my situation. The only thing I needed to do was to import a model from my __init__.py
file in the top level of an app. I had to move my imports into the functions using the model.
Django seems to have some weird code that can fail like this in so many different scenarios!
回答 24
我今天也遇到了这个错误。该消息引用了INSTALLED_APPS中我的应用程序中的某些特定应用程序。但实际上,它与此特定的应用程序无关。我使用了一个新的虚拟环境,却忘记安装一些在该项目中使用的库。在我安装了附加库之后,它开始工作了。
I got this error also today.
The Message referenced to some specific app of my apps in INSTALLED_APPS. But in fact it had nothing to do with this specific App. I used a new virtual Environment and forgot to install some Libraries, that i used in this project. After i installed the additional Libraries, it worked.
回答 25
对于PyCharm用户:使用“不干净”的项目结构时出现错误。
是:
project_root_directory
└── src
├── chat
│ ├── migrations
│ └── templates
├── django_channels
└── templates
现在:
project_root_directory
├── chat
│ ├── migrations
│ └── templates
│ └── chat
├── django_channels
└── templates
这里有很多好的解决方案,但是我认为,首先,您应该在设置之前清理项目结构或调整PyCharm Django设置 DJANGO_SETTINGS_MODULE
变量等。
希望对别人有帮助。干杯。
For PyCharm users: I had an error using not “clean” project structure.
Was:
project_root_directory
└── src
├── chat
│ ├── migrations
│ └── templates
├── django_channels
└── templates
Now:
project_root_directory
├── chat
│ ├── migrations
│ └── templates
│ └── chat
├── django_channels
└── templates
Here is a lot of good solutions, but I think, first of all, you should clean your project structure or tune PyCharm Django settings before setting DJANGO_SETTINGS_MODULE
variables and so on.
Hope it’ll help someone. Cheers.
回答 26
问题是:
您已经对模型文件进行了修改,但尚未将其添加到数据库中,但是您正在尝试运行Python manage.py runserver。
运行Python manage.py makemigrations
Python manage.py迁移
现在,Python manage.py runserver和一切都应该没问题。
The issue is that:
You have made modifications to your models file, but not addedd them yet to the DB, but you are trying to run Python manage.py runserver.
Run Python manage.py makemigrations
Python manage.py migrate
Now Python manage.py runserver and all should be fine.