How the Query Builder uses Python Descriptors
masterThe query builder leverages Python descriptors to provide a dual-purpose API. This allows the same attribute name to behave differently depending on whether it is accessed via a class or an instance:
- Class-level access: Accessing an attribute on the class (e.g.,
User.friends) returns a query builder path (AST nodes) used for constructing queries. - Instance-level access: Accessing the same attribute on an instance (e.g.,
user.friends) returns the actual data loaded from the database.
This pattern enables intuitive syntax like User.friends.name for building queries.