How to connect MySQL with Django SQLalchemy?

I'm trying to connect Django app with MySQL,

My model:

**models.py**

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.orm import relationship
from sqlalchemy.orm import sessionmaker


engine = create_engine('mysql://root:root0000@localhost', echo=True)
Base = declarative_base()
Session = sessionmaker(bind=engine)
Session.configure(bind=engine)
session = Session()


class School(Base):
    __tablename__ = 'schools'

    id = Column(Integer, primary_key=True)
    title = Column(String)
    address = Column(String)
    created = Column(DateTime(timezone=True), server_default=func.now())

My Controller:

views.py

class SchoolViewSet(ViewSet): query = models.session.query(models.School).all()

def list(self, request): return Response(self.query)

Getting this error:

in raise_mysql_exception
    raise errorclass(errno, errval)
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1046, 'No database selected')
Back to Top