From 5a7e1776dbe5b6a586b6a0b241155bbacb0767dd Mon Sep 17 00:00:00 2001 From: Conrad Date: Mon, 13 May 2024 11:21:25 +0200 Subject: [PATCH] fix: Added option to specify lookup_column for get_object_or_404 --- CHANGELOG.md | 6 +++++- creyPY/fastapi/crud.py | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0371073..f9f90a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,11 @@ All notable changes to this project will be documented in this file. -## 1.2.X +## 1.2.3 + +- Added option to specify lookup_column for get_object_or_404 + +## 1.2.2 - Added order_by method diff --git a/creyPY/fastapi/crud.py b/creyPY/fastapi/crud.py index 63ec901..9034355 100644 --- a/creyPY/fastapi/crud.py +++ b/creyPY/fastapi/crud.py @@ -10,8 +10,8 @@ from .models.base import Base T = TypeVar("T", bound=Base) -def get_object_or_404(db_class: Type[T], id: UUID | str, db: Session, expunge: bool = False) -> T: - obj = db.query(db_class).filter(db_class.id == id).one_or_none() +def get_object_or_404(db_class: Type[T], id: UUID | str, db: Session, expunge: bool = False, lookup_column: str = "id") -> T: + obj = db.query(db_class).filter(getattr(db_class, lookup_column) == id).one_or_none() if obj is None: raise HTTPException(status_code=404, detail="The object does not exist.") if expunge: