I am practicing using mysql in django and wanted to run a simply query on a database that I set up in my settings.py. I am connected to the database and can run the query fine in the manage.py shell. However, when I run the same code in a python file it does not work. Does anybody know why the query does not work in the python file?
query :
from django.db import connections
def query(id):
c = connections['default'].cursor()
c.execute("SELECT * FROM `peptides_proteins_000005 WHERE protein_id=%s;",[id])
rows = c.fetchone()
return rows
print(query(4519))
In the shell I will get a row back with the information I want however, when run in the python file I get this error:
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Is there any way to run this command in views.py or another python file without using inspectdb to put my database in models?