Skip to main content
Now, Let me create various model and integrate fastdaisy-admin into fastapi application.
import enum
from sqlalchemy import Column, Boolean, Integer, String, create_engine, Text, DATETIME, Enum
from sqlalchemy.orm import declarative_base, relationship
from sqlalchemy import ForeignKey
from fastapi import FastAPI
from fastdaisy_admin import Admin, ModelView

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

class Status(enum.Enum):
    AVAILABLE = "AVAILABLE"
    UNAVAILABLE = "UNAVAILABLE"

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    name = Column(String)
    email = Column(String)
    password = Column(String)
    address = Column(String)
    age = Column(Integer)
    books = relationship("Book", back_populates="author", cascade="all, delete-orphan")
    is_admin = Column(Boolean, default=False)

class Book(Base):
    __tablename__ = 'books'

    id = Column(Integer, primary_key=True)
    title = Column(String)
    description = Column(Text)
    author_id = Column(Integer, ForeignKey('users.id'))
    author = relationship("User", back_populates="books")
    status = Column(Enum(Status))
    code = Column(Integer, default=1)
    is_new = Column(Boolean, default=True)
    created_at = Column(DATETIME)

Base.metadata.create_all(engine)  # Create tables


# ---------------  Admin registration ---------------

app = FastAPI()
secret_key = "secret_key"
admin = Admin(app, secret_key, engine)

class UserAdmin(ModelView):
    model = User
    column_list = [User.id, User.name]

class BookAdmin(ModelView):
    model = Book

admin.add_view(UserAdmin)
admin.add_view(BookAdmin)

Permissions

You can configure a few general permissions for this model. The following options are available:
  • can_create: Allow to create new instances. Default value is True.
  • can_edit: Allow to edit created instances. Default value is True.
  • can_delete: Allow to delete model instance. Default value is True.
  • only_view: Only allow to view instance . Default value is False.
  • can_export: The model data can be exported in the list page. Default value is True.

class UserAdmin(ModelView):
    model = User
    can_create = True
    can_edit = True
    can_delete = False
    only_view = False

Metadata

The metadata for the model. The options are:
  • name: Display name for this model. Default value is the class name.
  • name_plural: Display plural name for this model. Default value is class name + s.
  • icon: Icon to be displayed for this model in the admin. Only FontAwesome supported.
  • category: Category name to display group of ModelView classes together in dropdown.
  • category_icon: Category icon to display.
  • divider_title: Divider title that separate Modelview or group of Modelview classes in sidebar.

class UserAdmin(ModelView):
    model = User
    name = "User"
    name_plural = "Users"
    icon = "fa-solid fa-user"
    category = "accounts"
    category_icon = "fa-solid fa-user"
    divider_title = 'Apps'