0

我正在尝试使用 HubSpot 的 Companies API 数据填充数据库。我在models.py 中创建了一个表。在views.py 文件中,我调用了API 数据,但是我无法将该数据保存在数据库中。我不明白我的代码中是否存在问题或其他任何问题?非常感谢您对这项任务的帮助。视图.py

def index(request):
url = "https://api.hubapi.com/companies/v2/companies?"

headers = {}
r = requests.get(url = url, headers ={'Authorization':'Bearer %s' %os.getenv('access_token')})
try:
    response_dict = json.loads(r.content)
    companies_list= response_dict['companies'] 
    
    
    for company in companies_list:

            Company.update_or_create(
            companyID = company['companyId'],
            sourceID = company['sourceId'],
            portalID = company['portalId'],
            country = company['country']
            
        )
     
except Exception as e:
    response_dict= "Data not found:"

return render(request, 'Apihub/index.html', {'response_dict': response_dict}

在模型类中:models.py

class Company(models.Model):
companyID = models.CharField(max_length=50, blank=True, null=True)
sourceID = models.CharField(max_length=20, blank=True, null=True)
portalID = models.IntegerField(default=0)
country = models.CharField(max_length=50, blank=True, null=True)
4

0 回答 0