I’m trying to make a django website that connects to an external MSSQL database to retrieve information only (read-only). This database is huge with hundreds of tables.
I can currently get it to work by creating a function in my django app that uses the connectionString and runs a raw SQL query and returns it in a pandas dataframe.
Somehow I feel this is not how Django should be used. Can you please guide me on how to do this in the proper Django way. I think I need to use models? but this is an external database and I don’t want to write anything to it. I also don’t want to have to define all the table names in my models, but I will if I have to. Can you please guide me on how to go about this while making sure the database stays as read-only?
How do I structure my settings.py,How do I structure my models.py,How do I structure my views.py to be able to interact with the db?
import pyodbc
import pandas as pd
def SQL_to_df(driver,server,database,user,password,query):
connectionString = pyodbc.connect(f"Driver={driver};Server{server};Database={database};UID={user};PWD={password};")
df = pd.read_sql(query, connectionString)
return df