Как подключить MySQL с помощью Django SQLalchemy?

Я пытаюсь соединить приложение Django с MySQL,

Моя модель:

**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())

Мой контроллер:

views.py

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

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

Получаем эту ошибку:

in raise_mysql_exception
    raise errorclass(errno, errval)
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1046, 'No database selected')
Вернуться на верх