There is a tiny moment in every Laravel developer's week that nobody talks about.

You are building a screen that lists users with their post counts. Your fingers type withCount('posts') before your brain has even finished the thought. No joins, no subqueries, no loading ten thousand rows just to call len() on them. It simply works, and you move on with your day.

That moment is exactly what I missed when I started writing Python backends full time, and it is a big part of why I'm building SwiftPY. So when I say version 0.4.2 is the release where the ORM started to feel like home, this is what I mean. Let me walk you through it.

Query results finally behave like collections

For a long time, await User.get() handed you a plain list. A list is fine, until you want to ask "does this contain that model?" or "give me the unique ones" or "hide the email column before I serialize this".

In 0.4.2, results come back as a ModelCollection. It is still a real Python list underneath, so nothing you already wrote breaks, but it grew some manners:

users = await User.all() users.contains(alice) # is this model in the collection? users.unique() # drop duplicate models users.make_hidden("email").to_dict() # serialize without exposing emails 

The same goes for the lists you get from relationships and eager loads, and slicing a collection gives you a collection back, not a bare list.

Count things without loading them

This is the withCount moment, translated into async Python:

users = await ( User.with_count("posts as post_total") .with_exists("orders as has_orders") .get() ) for user in users: print(user.name, user.post_total, user.has_orders) 

with_count, with_exists, with_sum, with_avg, with_min and with_max all compile into subqueries, so the database does the arithmetic and you load zero related rows. You can name the result with as, filter it with a callback, and it even handles self referential tables.

Eager loading got smarter too. You can ask for only the columns you need, and SwiftPY quietly keeps the foreign keys it needs to wire everything together:

users = await User.with_("posts:id,title").get() 

Asking better questions

Nested conditions used to mean reaching for raw SQL. Now you can group with a plain callback, and the generated SQL gets its parentheses right:

users = await ( User.where("active", True) .where(lambda q: q.where("role", "admin").or_where("role", "editor")) .get() ) # WHERE active = true AND (role = 'admin' OR role = 'editor') 

The same idea works for existence checks. where_has takes a callback to constrain the relationship you are asking about:

authors = await User.where_has("posts", lambda q: q.where("published", True)).get() 

I also added where_attached_to() and where_relation() for the common "give me everything connected to this specific model" questions.

Relationships with opinions

A relationship can now carry its own rules, and those rules are copy on chain, so the original is never mutated. That matters, because this kind of thing is where subtle bugs love to live.

class User(Model): posts = HasMany(Post, foreign_key="user_id") latest_posts = ( HasMany(Post, foreign_key="user_id") .order_by("created_at", "DESC") .limit(3) ) 

The interesting part is what happens when you eager load latest_posts for many users at once. A naive LIMIT 3 would give you three posts in total. SwiftPY uses a PostgreSQL window function (ROW_NUMBER() OVER (PARTITION BY ...)) so you get the latest three per user, in a single query. And yes, the ORDER BY survives the windowing, which was a bug I fixed in this release.

There is also chaperone() for the classic parent and child back reference problem, with cycle safe serialization so to_dict() does not recurse forever, plus with_attributes() for default values applied to everything you create through a relationship.

Pivot tables that finally tell you something

Many to many relationships always had this awkward truth: the interesting data often lives on the pivot table. A role on a membership, a joined date, a sort order.

teams = ( BelongsToMany(Team, ...) .with_pivot("role") .with_timestamps() .as_("membership") .order_by_pivot("created_at") ) 

Custom pivot columns and timestamps now come back on both eager and lazy loads, attach() writes the timestamps for you, and sync() does batched inserts instead of one query per row. If you happen to have a real column named pivot, nothing changes for you unless you opt into .as_("pivot").

The fix I'm most glad about

Not everything in this release is a shiny feature. One is a security fix, and it is the kind of bug that makes you pause.

Models can hide sensitive fields with __hidden__, and you can temporarily show them with make_visible(). Imagine an admin export where you briefly reveal a field. The problem: that temporary visibility could flow into the authenticated user object, and from there into the context and the JWT user payload.

Now hidden and sensitive keys are stripped unconditionally whenever an authenticated user is built from a model. A temporary make_visible() can no longer leak credentials into authentication state. A related fix ensures that fields you made visible on purpose do show up properly in JSON responses, instead of being stripped a second time.

The quiet improvements

A few things that will not make a headline but matter if you use the ORM daily:

  • refresh() can now reload nested relationships and cleans out stale aggregate attributes
  • HasOne gets the same per parent windowing as HasMany when eager loading
  • BelongsToMany qualifies columns so pivot tables no longer cause ambiguity errors
  • create() and create_many() on relationships run inside a transaction and respect your schema defaults
  • ModelCollection refuses to probe private and dunder attributes
  • The remaining Pyrefly warnings in the pool and query builder are gone, so your editor stays quiet

Where SwiftPY stands

SwiftPY is still in beta, and the ORM is the part with the largest surface area, which means it is the part where your project will find things mine does not. Different schemas, different naming habits, odd relationship shapes: that is exactly the diversity a 1.0.0 needs.

If you want to help, here is the lowest effort version:

  1. Pick a small project, or an existing schema you know well.
  2. Model it with SwiftPY and try the relationship and aggregate helpers.
  3. Open an issue for anything confusing, slow or wrong. "This surprised me" counts.

The code lives at https://github.com/Obrainwave/swiftpy. If you would rather talk first, send me a DM with "SWIFTPY TESTER" and I'll help you get set up. If you find a security problem, please message me privately before posting it publicly.

Building an ORM is easy to start and hard to finish. I'd love your help with the finishing part.

SwiftPY: Building towards 1.0.0.