AutoSkill FastAPI Generic Dynamic Filtering with Pydantic and SQLAlchemy
Implements a generic, reusable filtering mechanism in FastAPI using Pydantic and SQLAlchemy. It avoids hard-coded field checks by using a configuration list of tuples, supporting string 'ilike' searches with comma-separated values and date range queries.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt3.5_8_GLM4.7/fastapi-generic-dynamic-filtering-with-pydantic-and-sqlalchemy" ~/.claude/skills/ecnu-icalk-autoskill-fastapi-generic-dynamic-filtering-with-pydantic-and-sqlalch && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt3.5_8_GLM4.7/fastapi-generic-dynamic-filtering-with-pydantic-and-sqlalchemy/SKILL.mdsource content
FastAPI Generic Dynamic Filtering with Pydantic and SQLAlchemy
Implements a generic, reusable filtering mechanism in FastAPI using Pydantic and SQLAlchemy. It avoids hard-coded field checks by using a configuration list of tuples, supporting string 'ilike' searches with comma-separated values and date range queries.
Prompt
Role & Objective
You are a FastAPI and SQLAlchemy expert. Your task is to implement a generic, reusable filtering mechanism for database queries that avoids hard-coding field names in conditional statements.
Operational Rules & Constraints
- Generic Filter Structure: Use a list of tuples to define filters, where each tuple contains
.(column, value, operator) - Filter Function: Create a single
function that iterates through the list.apply_filters(query, filters) - String Matching ('ilike'): For string fields, split the value by commas to support multiple keywords. Use
combined withcolumn.ilike(f'%{v}%')
logic.or_ - Date Range ('daterange'): For date fields, split the value by commas.
- If one date is provided, filter for exact match.
- If two dates are provided, use
for the range.BETWEEN
- Code Readability: Ensure the code is concise and modular, avoiding repetitive
blocks for every specific field.if filter.field:
Anti-Patterns
- Do not write separate
,apply_name_filter
functions.apply_email_filter - Do not hard-code field names inside the main filtering loop logic; rely on the passed column object.
Triggers
- generic fastapi filtering
- dynamic sqlalchemy filter
- avoid case by case filter code
- fastapi date range query
- pydantic generic filter