aminalaee / sqladmin

SQLAlchemy Admin for FastAPI and Starlette
https://aminalaee.dev/sqladmin/
BSD 3-Clause "New" or "Revised" License
1.7k stars 174 forks source link

Multiple Admin Instances result in wrong URL's to the views #738

Open hochstibe opened 3 months ago

hochstibe commented 3 months ago

Checklist

Describe the bug

I have an application with the option of extensions. My idea was to have an admin page for the main application and separate admin pages for the extensions.

You can have multiple instances of Admin running, but the links to the Views on the left point to wrong URL's.

  1. Start the app

  2. The Views are working correctly, when accessing with the url

    1. http://localhost:8000/admin1/user1/list
    2. http://localhost:8000/admin2/user2/list
  3. The navigation results in wrong url

    1. http://localhost:8000/admin2 --> Click on User2s -> Wrong URL http://localhost:8000/admin1/user2/list

Steps to reproduce the bug


from fastapi import FastAPI
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.orm import declarative_base
from sqladmin import Admin, ModelView

# DB -------------------------------------------------------------------------
Base = declarative_base()
engine = create_engine(
    "sqlite:///example.db",
    connect_args={"check_same_thread": False},
)

class User1(Base):
    __tablename__ = "users1"

    id = Column(Integer, primary_key=True)
    name = Column(String)

class User2(Base):
    __tablename__ = "users2"

    id = Column(Integer, primary_key=True)
    name = Column(String)

Base.metadata.create_all(engine)  # Create tables

# Main App -------------------------------------------------------------------
app = FastAPI()

# Admin ----------------------------------------------------------------------
admin1 = Admin(app, engine, base_url="/admin1")
admin2 = Admin(app, engine, base_url="/admin2")

class UserAdmin1(ModelView, model=User1):
    column_list = [User1.id, User1.name]

class UserAdmin2(ModelView, model=User2):
    column_list = [User2.id, User2.name]

admin1.add_view(UserAdmin1)
admin2.add_view(UserAdmin2)

Expected behavior

No response

Actual behavior

No response

Debugging material

No response

Environment

Tested on Windows, I could also test on Linux

Additional context

No response

hochstibe commented 3 months ago

It is not related to multiple fastapi applications. I simplified the code to 1 Fastapi with 2 Admin