Hi Django community,
(I am new to this forum, I tried my best to formulate my question)
Context: I study bioinformatics and for my minor ‘webapplication development’ my project group and I have to built a webapplication using Django. I am responsible to create models and write code that reads, parses and inserts data (using MySQL) into these models. The data comes from a pipeline, using Snakemake, and it has to be inserted automatically into these models. I wrote this code in views.py that does the trick using dummy data (see code below for an example).
def insert_data_into_database():
"""location entiteit"""
with open(snakemake.output.a, 'r') as tsv:
next(tsv)
for line in csv.reader(tsv, delimiter='\t'):
with open('C:\\Users\\Aron\\PycharmProjects\\bpexi\\bpexi\\lysteria\\%s' % (line[2]), 'rb') as image:
binary_data = image.read()
Location.objects.get_or_create(location_id=line[0],
description=line[1],
location_image=binary_data)
My problem is: I have to read, parse and insert a lot of different tab seperated files, so views.py becomes cluttered with my code. This feels kind of counter intuitive.
My question is: What are good practices to insert data, from a pipeline, automatically into my models? I tried to create another file into my app directory, but that does not seem to work due to the fact I can not import my models for some reason (see code and error below).
code:
from .models import Sample
error:
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package
or
code:
from models import Sample
error:
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.