mirror of
https://github.com/creyD/creyPY.git
synced 2026-04-15 12:50:32 +02:00
fix: Added option to specify lookup_column for get_object_or_404
This commit is contained in:
@@ -2,7 +2,11 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
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
|
- Added order_by method
|
||||||
|
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ from .models.base import Base
|
|||||||
T = TypeVar("T", bound=Base)
|
T = TypeVar("T", bound=Base)
|
||||||
|
|
||||||
|
|
||||||
def get_object_or_404(db_class: Type[T], id: UUID | str, db: Session, expunge: bool = False) -> T:
|
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(db_class.id == id).one_or_none()
|
obj = db.query(db_class).filter(getattr(db_class, lookup_column) == id).one_or_none()
|
||||||
if obj is None:
|
if obj is None:
|
||||||
raise HTTPException(status_code=404, detail="The object does not exist.")
|
raise HTTPException(status_code=404, detail="The object does not exist.")
|
||||||
if expunge:
|
if expunge:
|
||||||
|
|||||||
Reference in New Issue
Block a user