为什么Django无法识别我导入的模型?

时间:2020-05-12 19:23:35

标签: python django

我有一个名为feeder的应用,它本身具有文件models.pyfeeder.py

现在,我想将模型StocksInfoStocksPrice导入到feeder.py中,以便使用获取的数据更新数据库。

它不以某种方式导入模型并引发错误。遗失的物品在哪里?

错误:

C:\Users\Jonas\AppData\Local\Programs\Python\Python38-32\python.exe C:/Users/Jonas/Desktop/CFD/CFD/feeder/feeder.py
Traceback (most recent call last):
  File "C:/Users/Jonas/Desktop/CFD/CFD/feeder/feeder.py", line 50, in <module>
    getStocksInfo();
  File "C:/Users/Jonas/Desktop/CFD/CFD/feeder/feeder.py", line 34, in getStocksInfo
    obj, created = StocksInfo.objects.update_or_create(
AttributeError: type object 'StocksInfo' has no attribute 'objects'

feeder.py

import requests

# import the required models does not work, they are greyed out + underlined red in IDE
from feeder.models import StocksInfo, StocksPrice

# Define stocksInfo Query Function
def getStocksInfo():

    # query stocksInfo and populate it to the database
    stocksInfo = requests.get('https://finnhub.io/api/v1/stock/profile2?symbol=AAPL&token=bqtcoffrh5re54ulvqig')
    stocksInfo = stocksInfo.json()

    # assign variables
    #TODO assign symbol to variable for populate check
    symbol = 'APPL'
    country = stocksInfo['country']
    exchange = stocksInfo['exchange']
    name = stocksInfo['name']
    ticker = stocksInfo['ticker']
    ipoDate = stocksInfo['ipo']
    marketCapitalization = stocksInfo['marketCapitalization']
    shareOutstanding = stocksInfo['shareOutstanding']
    logo = stocksInfo['logo']
    url = stocksInfo['weburl']
    industry = stocksInfo['finnhubIndustry']

    # update database
    obj, created = StocksInfo.objects.update_or_create(
        symbol=symbol,
        defaults={
            'country': country,
            'exchange': exchange,
            'name': name,
            'ticker': ticker,
            'ipoDate': ipoDate,
            'marketCapitalization': marketCapitalization,
            'shareOutstanding': shareOutstanding,
            'logo': logo,
            'url': url,
            'industry': industry,
        }
    )

getStocksInfo();

models.py

from django.db import models

class StocksInfo(models.Model):
    symbol = models.CharField(max_length=10, blank=False)
    country = models.CharField(max_length=50, blank=False)
    exchange = models.CharField(max_length=50, blank=False)
    name = models.CharField(max_length=50, blank=False)
    ticker = models.CharField(max_length=50, blank=False)
    ipoDate = models.CharField(max_length=50, blank=False)
    marketCapitalization = models.FloatField(max_length=30, blank=False)
    shareOutstanding = models.FloatField(max_length=30, blank=False)
    logo = models.CharField(max_length=150, blank=False)
    url = models.CharField(max_length=150, blank=False)
    industry = models.CharField(max_length=50, blank=False)

    StocksInfo = models.Manager()

    class Meta:
        db_table = 'StocksInfo'


class StocksPrice(models.Model):
    priceO = models.FloatField(max_length=15, blank=False)
    priceH = models.FloatField(max_length=15, blank=False)
    priceL = models.FloatField(max_length=15, blank=False)
    priceC = models.FloatField(max_length=15, blank=False)
    pricePC = models.FloatField(max_length=15, blank=False)

    StocksPrice = models.Manager()

    class Meta:
        db_table = 'StocksPrice'

没有要运行的迁移,我的应用feeder已包含在项目的settings.py中。

3 个答案:

答案 0 :(得分:1)

您正在用stocksInfo的结果覆盖requests.get变量,因此,当您尝试调用stocksInfo.objects时,实际上是从请求中引用JSON dict,而不是模型。

我建议对模型使用大写名称,以避免出现此类错误。

答案 1 :(得分:0)

您已经将ta模型名称覆盖为通过get方法从url接收的数据, 那意味着在您使用stocksInfo作为从api url获得的数据的真实名称命名之后,stocksInfo不再是模型的名称,对模型和数据来自使用单独的名称 requests.get('https://finnhub.io/api/v1/stock/profile2?symbol=AAPL&token=##########') 您可以按以下方式将数据保存到stocksInfo模型

导入请求

导入所需的模型不起作用,它们在IDE中显示为灰色+带下划线的红色

从feeder.models导入stocksInfo,stocksPrice

定义stocksInfo查询功能

def stocksInfo():

# Query stocksInfo and populate it to the database
stocks = requests.get('https://finnhub.io/api/v1/stock/profile2?symbol=AAPL&token=###############')
stocks= stocksInfo.json()

# Assign data to variables    
symbol = 'APPL'
country = stocks['country']
exchange = stocks['exchange']
name = stocks['name']
ticker = stocks['ticker']
ipoDate = stocks['ipo']
marketCapitalization = stocks['marketCapitalization']
shareOutstanding = stocks['shareOutstanding']
logo = stocks['logo']
url = stocks['weburl']
industry = stocks['finnhubIndustry']

# create object and update database
obj, created = stocksInfo.objects.update_or_create(
    symbol=symbol,
    defaults={
        'country': country,
        'exchange': exchange,
        'name': name,
        'ticker': ticker,
        'ipoDate': ipoDate,
        'marketCapitalization': marketCapitalization,
        'shareOutstanding': shareOutstanding,
        'logo': logo,
        'url': url,
        'industry': industry,
    }
)

stocksInfo();

答案 2 :(得分:0)

我从模型中删除了Managers并再次迁移,现在它可以工作了……但是,老天爷,我不知道定制管理器是什么原因导致它返回了错误。有什么想法吗?