코딩캠프/AI 웹개발 취업캠프
[AI 웹개발 취업캠프] 67Day - 프로젝트 18일차
고랑E
2023. 10. 25. 23:36
728x90
모델 생성
모델 생성 할때 받을 데이터 정의 해주고
schemas.py
class ModelCreate(BaseModel):
userId: int
title: str
private: bool
file: str
description: str
main.py
# 상단에 create_model 및 ModelCreate 추가
from BE.crud import create_user, get_user, verify_password, get_user_info, update_user_info, get_models, get_my_models, create_model
from BE.schemas import UserCreate, UserLogin, UserUpdate, ModelCreate
@app.post("/models")
def create_model(model: ModelCreate, db: Session = Depends(get_db)):
create_model(db, model)
return JSONResponse(content={"message": "모델 생성이 완료되었습니다."}, status_code=201)
crud.py
# 상단에 ModelCreate 추가
from BE.schemas import UserCreate, UserUpdate, ModelCreate
def create_model(db: Session, model: ModelCreate):
db_model = Model(**model.model_dump())
db.add(db_model)
db.commit()
db.refresh(db_model)
return db_model
하고 서버를 실행해서 하는데~
첫번째 문제 발생 : 함수 중복 사용으로 재귀 호출
다음과 같이 new로 수정해서 오류를 해결했다.
@app.post("/models")
def create_new_model(model: ModelCreate, db: Session = Depends(get_db)):
create_model(db, model)
return JSONResponse(content={"message": "모델 생성이 완료되었습니다."}, status_code=201)
두번째 문제 발생 : 잘못된 인자 사용?? userIId / user_id
인자로는 userId로 받고있고 db에는 user_id로 되어있어서 발생한 오류이다
원래 있던 userId를 pop 으로 꺼내고 삭제를 해주고 새로운 user_id 에 그 값을 넣어준다.
if 'userId' in model_data:
model_data['user_id'] = model_data.pop('userId')
그래서 아래와 같은 코드가 되었다.
def create_model(db: Session, model: ModelCreate):
model_data = model.model_dump()
if 'userId' in model_data:
model_data['user_id'] = model_data.pop('userId')
db_model = Model(**model_data)
db.add(db_model)
db.commit()
db.refresh(db_model)
return db_model
모든 오류를 해결하고 데이터를 넣어봤다.

모델 조회
기존에 만들었던 개인 모델 가져오는 코드에서 달라진 점은
- userId 대신 모델의 id로 가져오는거
- .all() 이 아닌 하나만 가져올 거니까 .first()
main.py
# 상단에 get_model 추가
from BE.crud import create_user, get_user, verify_password, get_user_info, update_user_info, get_models, get_my_models, create_model, get_model
@app.get("/models/{modelId}")
def read_model(modelId: int, db: Session = Depends(get_db)):
db_model = get_model(db, modelId)
return db_model
crud.py
def get_model(db: Session, modelId: int):
return db.query(Model).filter(Model.id == modelId).first()
후 가져 실행 결과

본 후기는 정보통신산업진흥원(NIPA)에서 주관하는 <AI 서비스완성! AI+웹개발 취업캠프 - 프론트엔드&백엔드> 과정 학습/프로젝트/과제 기록으로 작성되었습니다.