Route Initial DB Lookup with Dependencies #6

Open
opened 2026-05-22 08:40:35 +00:00 by chris · 0 comments
Owner

Most routes start with something like this:

org_model = db.query(Org).filter(Org.id == org_id).first()
if org_model is None:
	raise HTTPException(status_code=404, detail="Organisation not found")

This can be replaced with a dependency like this:

def get_org_model(db: db_dependency, org_id: Annotated[int, Path(gt=0)]) -> type[Org]:
	org_model = db.query(Org).filter(Org.id == org_id).first()
	if org_model is None:
		raise HTTPException(status_code=404, detail="Organisation not found")

	return org_model

org_model_dependency = Annotated[type[Org], Depends(get_org_model)]
Most routes start with something like this: ``` org_model = db.query(Org).filter(Org.id == org_id).first() if org_model is None: raise HTTPException(status_code=404, detail="Organisation not found") ``` This can be replaced with a dependency like this: ``` def get_org_model(db: db_dependency, org_id: Annotated[int, Path(gt=0)]) -> type[Org]: org_model = db.query(Org).filter(Org.id == org_id).first() if org_model is None: raise HTTPException(status_code=404, detail="Organisation not found") return org_model org_model_dependency = Annotated[type[Org], Depends(get_org_model)] ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: sr2/cloud-api#6
No description provided.