Skip to content

How could I put the admin code in a custom Python file named admin.py instead of the file where the FastAPI instance is located? #749

Answered by im-redactd
ChengChe106 asked this question in Q&A
Discussion options

You must be logged in to vote

You can do this now and it's fairly easy.

folder structure for my example is

app/
- admin/
-- setup.py
-- views/
--- user_admin.py
main.py

In your main.py

from app.admin.setup import init_admin
admin = init_admin(app)

your app/admin/setup.py would look like this

from sqladmin import Admin
from fastapi import FastAPI
from src.database import engine
from app.admin.views.user_admin import UserAdmin

def init_admin(app: FastAPI):
    admin = Admin(app, engine, title="App Title", templates_dir="app/templates")
    admin.add_view(UserAdmin)
    return admin

your app/admin/views/user_admin.py - you may not need to go to this level of organization if you don't have a lot of views and you coul…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@aminalaee
Comment options

@ChengChe106
Comment options

@im-redactd
Comment options

Answer selected by ChengChe106
@ChengChe106
Comment options

@aminalaee
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants