Showing data on frontend from sql databases using django

For fetching data from databases, I usually see people create a class in and they migrate it, and then they fetch data from databases and show it on the front-end. my question is, i already have a database which has student_info table and has lots of data. I don’t want to define student_info class in and do migration. Instead, since i already have student_info data in sql database. so I want to fetch it directly. can i do so?

one method i know of doing the same, is directly connecting sql server in as;


and fetching the data and passing it as variable in index.html. But the problem is, it fetches data only once, and if i refresh the page where local server is running, all the content will disappear, and server will report Broken pipe from ('', 59579) and to see the data, again i need to restart the sever.

Kindly looking for a better approach to show data on front-end from the existing databases

For the solution, you could do something simple.

Create a StudentData(models.Model) class that will store your data as model objects in them and define all the attributes/columns in models based on the data in the student info table.
Create a simple script (using pymysql) to retrieve all of the data from the student table and save it to a text file.
Take that text file and run a shell command or write a new script in your Django project to transfer the text file student data to model objects and save them. You now have your data.
This should work fine, in my opinion. If you want code as well. I’ll incorporate it.
first_file : “data”

import pymysql as psql
import os
connection = psql.connect(
    user = "root" , 
    password = os.environ.get("MYSQL_PASSWORD") , 
    database = "college" ,    
cursor = connection.cursor()
cursor.execute("select * from student") ;
data_text_file = open("Creating Database model/text_data.txt" , "w")

for row in cursor :
    temp_string = ""
    for data in row :
        temp_string+= "{} ".format(str(data)) 

I used pymysql for these scripts, but I believe mysqlconnector will also work.

Make a folder called “Creating Database Model” within your project.
To connect, enter your password. I’m using a college database with a student table containing some student data. Then I’m going to make a file called text data.txt that will contain the table’s text data. I used string formatting for convenience and better data retrieval on the other side.

So, according to this database, my is here. You design your own based on your requirements.

with open("Creating Database model/text_data.txt") as f :
    data =
new_data_list = []

data = data.split("\n")
new_data_list = []
for i in data :
    temp_list = i.split(" ")[:-1]
data = new_data_list[:-1]

for i in data :
    student_info = StudentModel(
        first_name = i[1] , 
        last_name = i[2] ,
        age = i[3] ,
        branch = i[4] 

All of these are in the “Creating Database Model” folder. Read this article on views in SQL from scaler topics for more information or clarification. Also, this can be valid solution, but maybe try first the Django legacy database integration.

This will complete all of your tasks. Before committing to the database, make sure to debug and understand.