Rustango docs

Filter a queryset at runtime

Problem

You need to build a filter from user input where the fields aren't known at compile time.

Chain .filter() calls (they AND together), and reach for the runtime Q tree when you need OR / dynamic composition.

use rustango::query::Q;

let mut qs = Article::objects().filter(Article::published.eq(true));
if let Some(tag) = params.tag {
    qs = qs.filter(Article::tag.eq(tag));
}
// OR across two columns at runtime:
qs = qs.filter(Q::eq("title", &q).or(Q::contains("body", &q)));
let rows = qs.order_by(&[("created_at", false)]).fetch(&pool).await?;

Compile-time checks

Prefer the Q!(Article.title__icontains = q) macro when the field is static — it validates the column + lookup at compile time.