I am trying to insert default data into table while running the migration.I am using flask migration to do this.
I am using below file/folder structure
/src
|_ admin/
|_ __init__.py
|_ forms.py
|_ views.py
|_ core/
|_ __init__.py
|_ forms.py
|_ views.py
|_ static/
|_ students/
|_ __init__.py
|_ forms.py
|_ views.py
|_ templates/
|_ admin/
|_ students/
|_ __init__.py
|_ models.py
|_ app.py
app.py
from src import app
if __name__ == '__main__':
app.run(debug=True)
models.py
from src import db
from werkzeug.security import check_password_hash,generate_password_hash
from flask_login import UserMixin
from datetime import datetime
class AdminUser(db.Model,UserMixin):
__tablename__ = 'admin_users'
id = db.Column(db.Integer,primary_key=True)
full_name = db.Column(db.String(50))
email = db.Column(db.String(50))
password = db.Column(db.String(128))
def __init__(self,fullname,email,password):
self.full_name = fullname
self.email = email
self.password = generate_password_hash(password)
def check_password(self,password):
return check_password_hash(self.password,password)
class Classes(db.Model):
__tablename__ = 'classes'
id = db.Column(db.Integer,primary_key=True)
class_name = db.Column(db.String(30))
def __init__(self,className):
self.class_name = className
class Divisions(db.Model):
id = db.Column(db.Integer,primary_key=True)
division_name = db.Column(db.String(30))
def __init__(self,divisionName):
self.division_name = divisionName
class Homework(db.Model):
__tablename__ = 'homeworks'
id = db.Column(db.Integer,primary_key=True)
subject = db.Column(db.String(50))
question = db.Column(db.String(500))
class_id = db.Column(db.Integer,db.ForeignKey('classes.id'),nullable=False)
division_id = db.Column(db.Integer,db.ForeignKey('divisions.id'),nullable=False)
last_date = db.Column(db.DateTime)
created_on = db.Column(db.DateTime,default=datetime.utcnow)
def __init__(self,subject,question,classId,divisionId,lastDate):
self.subject = subject
self.question = question
self.class_id = classId
self.division_id = divisionId
self.last_date = lastDate
class Student(db.Model,UserMixin):
__tablename__ = 'students'
id = db.Column(db.Integer,primary_key=True)
full_name = db.Column(db.String(50))
email = db.Column(db.String(50))
mobile = db.Column(db.String(15))
password = db.Column(db.String(128))
class_id = db.Column(db.Integer,db.ForeignKey('classes.id'),nullable=False)
division_id = db.Column(db.Integer,db.ForeignKey('divisions.id'),nullable=False)
def __init__(self,fullName,emailAddress,mobileNumber,classID,divisionID):
self.full_name = fullName
self.email = emailAddress
self.mobile = mobileNumber
password = mobileNumber[6:]
self.password = generate_password_hash(password)
self.class_id = classID
self.division_id = divisionID
def check_password(self,password):
return check_password_hash(self.password,password)
class StudentHomeWorks(db.Model):
__tablename__ = 'student_homework'
id = db.Column(db.Integer,primary_key=True)
student_id = db.Column(db.Integer,db.ForeignKey('students.id'),nullable=False)
homework_id = db.Column(db.Integer,db.ForeignKey('homeworks.id'),nullable=False)
answer = db.Column(db.Text)
image = db.Column(db.String(100))
submitted_date = db.Column(db.Date,default=datetime.utcnow)
def __init__(self,studenID,homeworkID,answer,image):
self.student_id = studenID
self.homework_id = homeworkID
self.answer = answer
self.image = image
src/init.py
from flask import Flask
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecretkey'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@localhost/student_mgmt'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
Migrate(app,db)
login_manager = LoginManager()
login_manager.init_app(app)
from src.core.views import homeBluePrint
from src.admin.views import adminBluePrint
from src.students.views import studentBluePrint
app.register_blueprint(homeBluePrint)
app.register_blueprint(studentBluePrint)
app.register_blueprint(adminBluePrint)
@login_manager.user_loader
def load_user(user_id):
from src.models import Student
student = Student.query.get(user_id)
return student
After surfing on the internet i found out that we can user before_first_request or seed. But couldn't understand how and where to use it.
Heres's the app.py
from src import app,db
from src.models import AdminUser
if __name__ == '__main__':
app.run(debug=True)
@app.before_first_request
def populate_db():
user = AdminUser(fullname='Admin',email='admin@admin.com',password='Admin@1234')
db.session.add(user)
db.session.commit()
The tables get created but data is not getting inserted.