亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將模型導入到由 manage.py 運行的文件時,“應用程序尚未加載”

將模型導入到由 manage.py 運行的文件時,“應用程序尚未加載”

阿波羅的戰車 2023-08-22 10:46:44
我想從外部 websocket 獲取加密貨幣數據并將其保存到數據庫。為了producer.py從頭開始運行,我將其添加INSTALLED_APPS到settings.py:INSTALLED_APPS = [    'crypto.spot.producer',]producer.py是:from .models import Spotasync def message(u, t, interval, timeout):    async with websockets.connect(uri=u + '?token=' + t, ping_interval=interval, ping_timeout=timeout) as websocket:        await websocket.send(json.dumps({"id": "4848226",                                         "type": "subscribe",                                         "topic": "/market/ticker:all",                                         "response": True}))        while True:            response = await websocket.recv()            result = json.loads(response)            '''            write result to django model (Spot)            '''while True:    response = r.post('https://api.kucoin.com/api/v1/bullet-public')    payload = json.loads(response.text)    uri = payload['data']['instanceServers'][0]['endpoint']    token = payload['data']['token']    ping_interval = payload['data']['instanceServers'][0]['pingInterval']    ping_timeout = payload['data']['instanceServers'][0]['pingTimeout']    asyncio.get_event_loop().run_until_complete(message(uri, token, ping_interval, ping_timeout))    asyncio.get_event_loop().run_forever()
查看完整描述

1 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

您收到此錯誤是因為在 django 加載完成之前無法訪問模型。您的模塊嘗試在模型尚未加載時訪問它們。


要解決您的問題,您應該創建一個 django 管理命令或一個加載和配置 django 的獨立腳本,之后您可以毫無問題地運行生產者循環并訪問模型。例如


producer.py模塊:


import sys, os, django

sys.path.append("/path/to/your/project")

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your-project.settings")

django.setup()



from <your-app>.models import Spot


.... Your script

不要忘記從 中刪除模塊 ( crypto.spot.producer) INSTALLED_APPS。


查看完整回答
反對 回復 2023-08-22
  • 1 回答
  • 0 關注
  • 3992 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號