Почему выдает ошибку в django model?
Выдает такую вот ошибку, в чем дело? RuntimeWarning: Accessing the database during app initialization is discouraged. To fix this warning, avoid executing queries in AppConfig.ready() or when your app modules are imported. warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning)
вот мой код в файле models.py:
from clickhouse_backend import models
import uuid
from main.parser import data
class Currency(models.ClickhouseModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
date = models.DateField()
unit = models.Int128Field()
rate = models.Float64Field()
class Meta:
engine = models.MergeTree(
order_by=["id"]
)
for el in data:
el_to_db = Currency(date = el['date'], unit=el['unit'], rate=el['rate'])
el_to_db.save()
также сам файл парсер:
from urllib.request import urlopen
from bs4 import BeautifulSoup
from pathlib import Path
from datetime import datetime
inner_html_code = str(urlopen('https://www.cbr.ru/currency_base/dynamics/?UniDbQuery.Posted=True&UniDbQuery.so=1&UniDbQuery.mode=1&UniDbQuery.date_req1=&UniDbQuery.date_req2=&UniDbQuery.VAL_NM_RQ=R01239&UniDbQuery.From=01.01.2025&UniDbQuery.To=20.12.2025').read(),'utf-8')
inner_soup = BeautifulSoup(inner_html_code, "html.parser")
inner_soup = inner_soup.find('div', {"class": 'table-wrapper'})
# очищаем код от выбранных элементов
def delete_div(code,tag,arg):
# находим все указанные теги с параметрами
for div in code.find_all(tag, arg):
# и удаляем их из кода
div.decompose()
delete_div(inner_soup, "div", {'class':'table-caption'})
delete_div(inner_soup, "td", {'colspan':'3'})
array = []
for tag in inner_soup.select('td'):
# добавляем адрес ссылки в нашу общую переменную
array.append(tag.get_text(strip=True))
array_str = str(array)
array_str = array_str.replace('[', '').replace(']', '').replace("'", "").replace(", ", " ").replace(",", ".")
file = open('array.txt', 'w')
file.write(array_str)
file.close()
def get_array_info(path):
file_with_array = open(path, 'r')
array_rows = file_with_array.readlines()
for array_rows in array_rows:
array_rows = array_rows.split(" ")
n=3
array_rows = [array_rows[i:i + n] for i in range(0, len(array_rows), n)]
rows_list = []
for row in array_rows:
row_dict = {}
row_dict['date'] = datetime.strptime(row[0], '%d.%m.%Y').date()
row_dict['unit'] = int(row[1])
row_dict['rate'] = float(row[2])
rows_list.append(row_dict)
file_with_array.close()
return rows_list
path_with_file = Path(r'C:\Users\Никита\Desktop\Django\Курс валют\venv\app\array.txt')
data = get_array_info(path_with_file)