-
gao55pan说:
用fastapi和sqlalchemy和amis构建一个学生管理的模块,要求通过admin后台实现所有的基础功能,包含增删改查,请给出完整的项目代码
from fastapi import FastAPI from sqlalchemy import create_engine from amis import Admin # 创建应用 app = FastAPI() # 创建数据库引擎 engine = create_engine('sqlite:///my_students_db.db') # 创建admin对象 admin = Admin(app, engine) # 注册学生模型 @admin.register(model_name='Student', db_type='sqlite', db_table='students') class Student(admin.Model): # 定义学生模型 id = admin.Integer(primary_key=True) name = admin.String(max_length=50) age = admin.Integer() gender = admin.String(max_length=10) address = admin.String(max_length=100) # 创建数据库表 admin.create_all() # 启动应用 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
-
gao55pan说:
用fastapi和sqlalchemy以及amis构建学生信息管理的模块,要求通过admin后台实现所有的基础功能,包含增删改查,要求目录结构合理,请给出完整的项目代码
# 项目代码 # 创建一个服务器 from fastapi import FastAPI # 导入sqlalchemy和amis from sqlalchemy import create_engine from amis import Admin # 创建一个fastapi应用 app = FastAPI() # 创建一个数据库引擎 engine = create_engine('sqlite:///students.db') # 创建一个admin后台 admin = Admin(app, engine, url='/admin') # 创建一个students表 admin.register('students', columns=[ 'name', 'age', 'gender', 'address', 'phone' ] ) # 启动服务器 if __name__ == '__main__': app.run(debug=True, port=8000)
-
gao55pan说:
请用fastapi和sqlalchemy以及amis构建出一套学生信息管理系统,要求可以通过admin后台实现对学生信息的增删改查,要求目录结构合理,请给出每个文件的完整代码
首先,确定学生信息管理系统的目录结构:
|--app.py
|--models
| |--base.py
| |--students.py
|--resources
| |--students.py
|--utils
| |--middlewares.pyfrom fastapi import FastAPI from amis import Admin admin = Admin(name="学生信息管理系统", prefix="/admin") app = FastAPI( title="学生信息管理系统", version="0.1.0", docs_url="/", openapi_url="/openapi.json", admin=admin ) from resources.students import student_routers app.include_router(student_routers, prefix="/students", tags=["students"])
- models/base.py
from sqlalchemy.ext.declarative import declarative_base Base = declarative_base()
- models/students.py
from sqlalchemy import Column, Integer, String from .base import Base class Student(Base): __tablename__ = "students" id = Column(Integer, primary_key=True) name = Column(String) age = Column(Integer) gender = Column(String)
- resources/students.py
from fastapi import APIRouter from amis import ModelViewSet from utils.middlewares import get_db from models.students import Student router = APIRouter() @router.get("/") def get_students(): session = get_db() students = session.query(Student).all() session.close() return {"students": students} student_viewset = ModelViewSet( model=Student, session=get_db, serializer_class=Student ) student_routers = student_viewset.router
- utils/middlewares.py
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker engine = create_engine("sqlite:///:memory:") Session = sessionmaker(bind=engine) def get_db(): return Session()
-
gao55pan说:
请用fastapi-amis-admin和sqlalchemy构建一套学生信息管理系统,要求可以通过admin后台实现对学生信息的增删改查,要求目录结构合理,请给出每个文件的完整代码
。
1. 构建项目结构
├── app.py ├── model.py └── config.py
2. model.py
from sqlalchemy import Column, Integer, String from config import engine, Base class Student(Base): __tablename__ = 'students' id = Column(Integer, primary_key=True) name = Column(String) age = Column(Integer) score = Column(Integer) Base.metadata.create_all(engine)
3. config.py
from sqlalchemy import create_engine # SQLite engine = create_engine('sqlite:///students.db', echo=True) # MySQL # engine = create_engine('mysql+pymysql://username:password@localhost:3306/database', echo=True) # PostgreSQL # engine = create_engine('postgresql://username:password@localhost/database', echo=True)
4. app.py
import time from fastapi import FastAPI from sqlalchemy.orm import Session from config import engine from model import Student from fastapi_amis_admin import AmisAdmin # 创建FastAPI实例 app = FastAPI() # 创建amis admin admin = AmisAdmin(app, engine=engine) # 注册Student Model admin.register( model=Student, name='学生信息', list_display=['id', 'name', 'age', 'score'], search_fields=['name', 'age', 'score'], form_fields=[ {'name': 'name', 'type': 'string', 'label': '姓名'}, {'name': 'age', 'type': 'number', 'label': '年龄'}, {'name': 'score', 'type': 'number', 'label': '分数'}, ] ) # 添加默认学生信息 @app.on_event("startup") def startup_event(): db = Session(bind=engine) exists = db.query(Student).filter_by(name='张三').first() if not exists: db.add(Student(name='张三', age=18, score=100)) db.commit() db.close()