Django:导入模型有效,此后立即导入其实例失败

时间:2019-03-15 04:42:06

标签: python django

我正在使用此仓库: https://github.com/IntelligentTrading/data

作为参考,我相信这两行中的后一条是令人反感的:

from apps.channel.models import ExchangeData
from apps.channel.models.exchange_data import POLONIEX

页面上有说明。我快要走到尽头了,然后运行:

  

python manage.py poloniex_polling

作为参考,这是poloniex_polling.py的代码:

import json
import logging
import schedule
import time

from django.core.management.base import BaseCommand
from requests import get, RequestException

from apps.channel.models import ExchangeData
from apps.channel.models.exchange_data import POLONIEX

logger = logging.getLogger(__name__)


class Command(BaseCommand):
    help = "Polls data from Poloniex every 1 minute"

    def handle(self, *args, **options):
        logger.info("Getting ready to poll Poloniex...")
        schedule.every(1).minutes.do(poll_latest_poloniex_data)

        keep_going=True
        while keep_going:
            try:
                schedule.run_pending()
                time.sleep(1)
            except Exception as e:
                logger.debug(str(e))
                logger.info("Poloniex polling shut down.")
                keep_going = False


def poll_latest_poloniex_data():
    try:
        logger.info("polling for Poloniex data...")
        req = get('https://poloniex.com/public?command=returnTicker')

        poloniex_data_point = ExchangeData.objects.create(
            source=POLONIEX,
            data=req.json(), # the exact json from the request data
            timestamp=time.time() # now
        )
        logger.info("Saving Poloniex data...")

    except RequestException:
        return 'Error to collect data from Poloniex'

运行python manage.py poloniex_polling时,我得到:

INFO:settings:Deployment environment detected: LOCAL
INFO:settings:Importing vendor_services_settings
INFO:settings:LOCAL environment detected. Importing local_settings.py
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/cerulean/.virtualenvs/ITF/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/cerulean/.virtualenvs/ITF/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/cerulean/.virtualenvs/ITF/lib/python3.6/site-packages/django/core/management/__init__.py", line 224, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/home/cerulean/.virtualenvs/ITF/lib/python3.6/site-packages/django/core/management/__init__.py", line 36, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/home/cerulean/.virtualenvs/ITF/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/cerulean/Code/ITF/data-fork/data/apps/channel/management/commands/poloniex_polling.py", line 10, in <module>
    from apps.channel.models.exchange_data import POLONIEX
ImportError: cannot import name 'POLONIEX'

好像

from apps.channel.models.exchange_data import POLONIEX

poloniex_polling.py中的实例试图在实例化之前导入名为POLONIEX的模型。

根据我在启动和运行该应用程序方面的经验,这通常是一个更高级别的问题。可能是数据库未运行?我正在将Postgresql服务器作为服务运行。数据库中有数据,并且localhost:8000正在运行。

这是models文件夹中的exchange_data.py的样子:

from django.db import models
from django.contrib.postgres.fields import JSONField
from unixtimestampfield.fields import UnixTimeStampField



    class ExchangeData(models.Model):
        source = models.CharField(max_length=128)
        data = JSONField(default="")
        timestamp = UnixTimeStampField(null=False)


        # MODEL PROPERTIES

        # MODEL FUNCTIONS

非常感谢。

2 个答案:

答案 0 :(得分:0)

这显然是该命令的错误。从the code中可以看出,该模型文件不包含名为POLONIEX的对象。没有丢失的数据库无法解决此问题。

您在“使用此应用程序中挣扎”的经历可能归因于它不起作用

答案 1 :(得分:0)

我知道了。我将代码添加到从相关存储库中提取的模型中,并修复了错误:

from settings import SOURCE_CHOICES

(POLONIEX, BITTREX) = list(range(2))
SOURCE_CHOICES = (
    (POLONIEX, 'poloniex'),
    (BITTREX, 'bittrex'),
)

现在,Poloniex对象存在。