diff --git a/docs/api_reference/model_view.md b/docs/api_reference/model_view.md index e464525f..87cd0b4e 100644 --- a/docs/api_reference/model_view.md +++ b/docs/api_reference/model_view.md @@ -49,7 +49,7 @@ - count_query - search_query - sort_query - - form_query + - edit_form_query - on_model_change - after_model_change - on_model_delete diff --git a/docs/configurations.md b/docs/configurations.md index 108a2823..10394ff0 100644 --- a/docs/configurations.md +++ b/docs/configurations.md @@ -200,7 +200,7 @@ The forms are based on `WTForms` package and include the following options: * `form_include_pk`: Control if primary key column should be included in create/edit forms. Default is `False`. * `form_ajax_refs`: Use Ajax with Select2 for loading relationship models async. This is use ful when the related model has a lot of records. * `form_converter`: Allow adding custom converters to support additional column types. -* `form_query`: A method with the signature of `(request) -> stmt` which can customize the form query. +* `edit_form_query`: A method with the signature of `(request) -> stmt` which can customize the edit form data. !!! example diff --git a/docs/cookbook/optimize_relationship_loading.md b/docs/cookbook/optimize_relationship_loading.md index 71872ea5..f7eb8fbb 100644 --- a/docs/cookbook/optimize_relationship_loading.md +++ b/docs/cookbook/optimize_relationship_loading.md @@ -61,19 +61,19 @@ class ParentAdmin(ModelView, model=Parent): form_excluded_columns = [Parent.children] ``` -### Using `form_query` to customize form query +### Using `edit_form_query` to customize the edit form data -If you would like to fully customize the query to populate the form, you may override the -`form_query` function with your own SQLAlchemy query. In the following example, overriding the -default query will allow you to filter relationships to show only relevant data. +If you would like to fully customize the query to populate the edit object form, you may override +the `edit_form_query` function with your own SQLAlchemy query. In the following example, overriding +the default query will allow you to filter relationships to show only related children of the parent. ```py class ParentAdmin(ModelView, model=Parent): - def form_query(self, request: Request) -> Select: + def edit_form_query(self, request: Request) -> Select: parent_id = request.path_params["pk"] return ( super() - .form_query(request) + .edit_form_query(request) .join(Child) .options(contains_eager(Parent.children)) .filter(Child.parent_id == parent_id) diff --git a/sqladmin/models.py b/sqladmin/models.py index f78abf48..37f868f8 100644 --- a/sqladmin/models.py +++ b/sqladmin/models.py @@ -808,10 +808,10 @@ async def get_object_for_details(self, value: Any) -> Any: return await self._get_object_by_pk(stmt) async def get_object_for_edit(self, request: Request) -> Any: - stmt = self.form_query(request) + stmt = self.edit_form_query(request) - if type(self).form_query == ModelView.form_query: - # If form_query fn hasn't been overridden, add all relationships + if type(self).edit_form_query == ModelView.edit_form_query: + # If edit_form_query fn hasn't been overridden, add all relationships for relation in self._form_relations: stmt = stmt.options(joinedload(relation)) @@ -1047,10 +1047,11 @@ def list_query(self, request: Request) -> Select: return select(self.model) - def form_query(self, request: Request) -> Select: + def edit_form_query(self, request: Request) -> Select: """ - The SQLAlchemy select expression used for the form page which can be customized. - By default it will select all objects without any filters. + The SQLAlchemy select expression used for the edit form page which can be + customized. By default it will select the object by primary key(s) without any + additional filters. """ identifier = request.path_params["pk"] diff --git a/tests/test_models.py b/tests/test_models.py index aed7de06..f04a419e 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -382,7 +382,7 @@ def list_query(self, request: Request) -> Select: assert len(await view.get_model_objects(request)) == 1 -async def test_form_query() -> None: +async def test_edit_form_query() -> None: session = session_maker() batman = User(name="batman") batcave = Address(user=batman, name="bat cave") @@ -396,7 +396,7 @@ class UserAdmin(ModelView, model=User): async_engine = False session_maker = session_maker - def form_query(self, request: Request) -> Select: + def edit_form_query(self, request: Request) -> Select: return ( select(self.model) .join(Address)