Skip to content

Commit

Permalink
Update references to edit_form_query
Browse files Browse the repository at this point in the history
Since the logic behind this query function requires the primary key(s),
it will not work with the create form. Updating the name and information
to reflect that it is intended for use with the edit form specifically.
  • Loading branch information
lukeclimen committed Apr 15, 2024
1 parent f357d47 commit 11122b7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/api_reference/model_view.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
- count_query
- search_query
- sort_query
- form_query
- edit_form_query
- on_model_change
- after_model_change
- on_model_delete
Expand Down
2 changes: 1 addition & 1 deletion docs/configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions docs/cookbook/optimize_relationship_loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions sqladmin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit 11122b7

Please sign in to comment.