[英]Error from Tasks job when referencing a model that uses from django.contrib.auth.models import User
我的 blog.models 正在使用:
from django.contrib.auth.models import User
model 是從我的腳本中調用的,我每小時作為任務運行一次。 這是我看到的錯誤:
Traceback (most recent call last):
File "/home/redinv/src/cron.py", line 8, in <module>
from blog.models import ImagePost, MyPost, ScheduledPost
File "/home/redinv/src/blog/models.py", line 3, in <module>
from django.contrib.auth.models import User
File "/usr/lib/python3.8/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/usr/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/usr/lib/python3.8/site-packages/django/db/models/base.py", line 103, in __new__
app_config = apps.get_containing_app_config(module)
File "/usr/lib/python3.8/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "/usr/lib/python3.8/site-packages/django/apps/registry.py", line 134, in check_apps_ready
settings.INSTALLED_APPS
File "/usr/lib/python3.8/site-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/usr/lib/python3.8/site-packages/django/conf/__init__.py", line 60, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
2020-06-25 00:50:25 -- Completed task, took 3.95 seconds, return code was 1.
在 INSTALLED_APPS = 下的設置文件中,我已經有了:
'django.contrib.auth',
這段代碼當然是在我的機器上本地運行的,我試圖弄清楚這里有什么問題?
我嘗試從兩個位置運行該腳本,以查看在嘗試查找導入文件時是否存在問題:
/home/username/src
/home/username/src/blog
兩者的結果相同。
感謝任何幫助,我是 python 的新手。
謝謝
如果要從網站上下文之外運行 Django 代碼,則需要先初始化 Django。 有兩種方法可以做到這一點:
setup
function 進行設置; Django 文檔的這一部分解釋了如何做到這一點
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.