Skip to content

Fastapi: how to avoid circular import when models are in a separate file - admin.init_app is missing for factory pattern #435

Answered by lesleslie
simonsax asked this question in Q&A
Discussion options

You must be logged in to vote

@simonsax, along the lines of what @aminalaee is saying here a couple of examples to avoid the circular import:

I have an authentication backend as well that requires I have the models being loaded before initializing the admin.

# main.py

from fastapi import FastAPI

app = FastApi()

@app.on_event("startup")
async def on_startup():
    import models
    from database import db_engine
    from sqladmin import Admin
    from my_auth_module import AppAuthBackend

    admin_backend = Admin(
        app=app_main,
        engine=db_engine,
        authentication_backend=AppAuthBackend(my_secret_key, models.Person),
    )

    # you can put the model view right here and no longer need the admin…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@simonsax
Comment options

Comment options

You must be logged in to vote
1 reply
@simonsax
Comment options

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