Configuring Alembic in Django Rest Framework with SQLAlchemy

20 views Asked by At

I have a DRF project where I am using SQLAlchemy along with Django's built-in ORM. For configuring Alembic into my project I did this.

Installed Alembic with pip install alembic

Then I've run this command on my root project directory. alembic init alembic

This command generated a folder called alembic and a file named alembic.ini in my root project directory. Inside that folder there are some files like (env.py, README, script.py.mako) and a folder named versions.

Now, inside the alembic.ini file, I've added this line,

sqlalchemy.url = sqlite3:///db.sqlite3

This is how I configured.

This is my models.py:

from sqlalchemy import create_engine, Column, String, Integer, Float, ForeignKey
from sqlalchemy.orm import declarative_base, sessionmaker, relationship
from django.contrib.auth import models


engine = create_engine('sqlite:///db.sqlite3')
Base = declarative_base()


class PlaceInfoModel(Base):
    __tablename__ = 'place_info'

    id = Column(Integer, primary_key=True, autoincrement=True)
    owner_id = Column(Integer,nullable=False)  
    name = Column(String(60))
    address = Column(String(300))
    rating = Column(Float)
    type = Column(String(20))
    image = Column(String)
    
    
Base.metadata.create_all(engine)


In my alembic/env.py I added these lines:

from review.models import Base, PlaceInfoModel
target_metadata = Base.metadata

But, whenever I run this alembic revision --autogenerate -m "Created SQLAlchemy Model" I am getting this error.

File "D:\SQLAlchemy Practice\env\Lib\site-packages\django\conf\__init__.py", line 69, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
0

There are 0 answers