View Reference#
This page lists everything you can set, override, or call on a view. CRUD Routes explains the default HTTP contract, and Request Lifecycle & Hooks explains which method to override. Modules documents the complete signatures.
View classes#
The view classes, and the result that a list returns:
Symbol |
Description |
|---|---|
Base class for all class-based views. Subclass this directly when you do not need CRUD; add endpoints with |
|
Supported advanced base class for custom CRUD foundations shared by sync and async views. Import from |
|
Async CRUD view. Use with async SQLAlchemy sessions. |
|
Sync CRUD view. Use with sync SQLAlchemy sessions. |
|
Value object returned by |
|
Async CRUD view that follows the |
|
Sync variant of |
Class attributes#
Every View subclass, CRUD or not, honors these class attributes:
Attribute |
Type |
Description |
|---|---|---|
|
URL prefix for all routes in the view (e.g. |
|
|
OpenAPI tags. When unset, Restly takes one from the prefix; see the entry. |
|
|
A list of FastAPI dependencies, such as |
|
|
OpenAPI response overrides. |
RestView and AsyncRestView add the following:
Attribute |
Type |
Description |
|---|---|---|
|
The view’s schema. Restly derives the response, create and update schemas from it. If omitted, generated from |
|
|
The class of everything that goes out: the responses, the list items, the fields the list params filter on, and the relationships the view loads. Derived by removing |
|
|
The body of |
|
|
The body of |
|
|
The SQLAlchemy mapped class. A class without a mapper raises at class definition. |
|
|
Type of the |
|
|
The routes to serve: |
|
|
Routes to leave out, after |
|
|
The list params (filter, sort, page) as a Pydantic model, generated from |
|
|
The filter keys to keep, as keys or patterns. |
|
|
The filter keys to leave out, after |
|
|
The keys that |
|
|
The sort keys to leave out, after |
|
|
The name of the sort parameter. |
|
|
The sort when the request has none, written as the client writes |
|
|
Where a sort puts NULLs. |
|
|
How list endpoints paginate, |
|
|
The clause every read applies, shared by |
|
|
What a list route does with a query key that it does not read. |
|
|
|
The view’s actions: the values that |
Pagination settings#
fr.NumberedPagination holds the
settings. Each is a keyword argument, and replace(**changes) returns a
checked copy:
Setting |
Default |
Purpose |
|---|---|---|
|
Page size when the client sends none. Lower it and cap |
|
|
Largest page size a client may ask for; a larger one is rejected with |
|
|
Largest page number a client may ask for; a larger one is rejected with |
|
|
Query parameter for the page number. |
|
|
Query parameter for the page size. |
|
|
|
The list response model: a generic model filled by field name from |
fr.NoPagination has one setting,
envelope, Envelope by
default, filled from data and total_count, the number of rows.
The envelope’s shape and custom alternatives are covered in Responses.
Endpoint decorators#
Use these decorators on methods in a view class:
Decorator |
HTTP Method |
Default Status |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The shorthand decorators explicitly set the default status code shown. Pass status_code= to override it.
Other keyword arguments pass through to FastAPI route registration: response_model=, dependencies=, responses=, tags=, and other APIRouter.add_api_route() options.
@fr.put(...) is available for custom endpoints, but the default update route
uses PATCH.
Methods#
Each CRUD route on RestView / AsyncRestView runs an endpoint method
(<verb>_endpoint), a final handler (handle_<verb>), and a business method
(create, update, delete) or a final load (get_one, get_many).
Request Lifecycle & Hooks shows the steps, and which
method to override for each change.
Alongside these are the declared read scope (the scope class
attribute) and cross-cutting override points (where_id, not_found,
get_loader_options, apply_list_params, apply_filters, count,
authorize, before_action_commit / after_action_commit, to_response,
snapshot). The handlers, the loads and select are final: you call them
rather than override them, and a view class that defines one fails at class
definition.
On AsyncRestView, the handlers, the loads, the business methods,
apply_list_params, apply_filters, count, authorize and the
transaction hooks are async def. The response methods, snapshot,
select, where_id and get_loader_options are plain def. not_found
may be either. The two brackets are entered with async with. Argument
names are identical between variants.
Kind |
Method |
Signature |
Return |
Purpose |
|---|---|---|---|---|
Endpoint method |
|
the list response, such as |
|
|
Endpoint method |
|
response class |
|
|
Endpoint method |
|
response class |
|
|
Endpoint method |
|
response class |
|
|
Endpoint method |
|
|
|
|
Handler |
|
|
Run |
|
Handler |
|
|
Load through |
|
Handler |
|
|
Authorize, check the references, run |
|
Handler |
|
|
Load, authorize, check the references, snapshot, run |
|
Handler |
|
|
Load, authorize, snapshot, run |
|
Custom-action bracket |
|
context manager |
Entered as |
|
Commit bracket |
|
context manager |
Share one commit across write actions on the session. The outermost block commits, then runs their after-hooks. See Commit several writes together. |
|
Load |
|
|
|
|
Load |
|
|
|
|
Load |
|
|
|
|
Override point |
|
|
The WHERE for an id from the path. The primary key by default. Override to find the row by another key, such as a slug; a model with a composite primary key needs an override for its item routes. It must compare columns of the view’s model. Plain |
|
Override point |
|
|
The exception that |
|
Business method |
|
|
Build a new object and return it, without a flush: the handler saves it. Commit-free: the usual create override point. |
|
Business method |
|
|
Apply the update payload to |
|
Business method |
|
|
Mark |
|
Override point |
|
|
Apply the list params to |
|
Override point |
|
|
Apply the generated filters and the view’s own filters to |
|
Decorator |
|
the method |
Make a view method the filter for the query key |
|
Override point |
|
|
Total for a paginated list. It gets the same statement with the list params applied, leaves out |
|
Override point |
|
|
Gate a verb. A no-op by default; override to enforce policy and raise |
|
Override point |
|
|
In-transaction side effect (outbox/audit rows), atomic with the write. |
|
Override point |
|
|
Post-commit side effect (email, webhook, cache invalidation). |
|
Override point |
|
response payload |
The method that builds every response, called by the endpoint methods with the |
|
Override point |
|
|
Frozen capture of an object’s already-loaded column values, taken after |
|
Override point |
|
|
The loader options of every read: by default a |
|
Helper |
|
instance of |
Validate and serialize one ORM object with Restly’s alias/reference/write-only handling. Override for custom projections or an intentional |
|
Helper |
|
envelope model instance |
Build the list response body: an instance of the pagination’s envelope, or of |
The scope argument of the loads and of select has the type
fr.views.ReadScope: None resolves the view’s scope, a clause replaces it,
and fr.clauses.UNSCOPED reads unscoped.
Functions#
Function |
Signature |
Return |
Purpose |
|---|---|---|---|
|
|
The scope that a read applies: the view’s |
Internal methods prefixed with _, such as _reject_unknown_query_params, are implementation details even though they are visible on instances.
See Views for the class hierarchy, Request Lifecycle & Hooks for examples of choosing which method to override, and Type Annotations for the typed signatures of these methods.
The same operations outside a view#
The default create and update call the functions in
fr.objects with self.session and
self.model. Use the fr.objects forms in custom routes that touch a model
other than self.model, and in services, workers, or tests. Each function has a sync and an async variant, such as
make_new_object and
async_make_new_object.