sqladmin icon indicating copy to clipboard operation
sqladmin copied to clipboard

Add ModelView.form_query property to create form-filtering feature

Open lukeclimen opened this issue 11 months ago • 2 comments

Checklist

  • [X] There are no similar issues or pull requests for this yet.

Is your feature related to a problem? Please describe.

In the list view for a model with relationships, I am able to override the ModelView.list_query property to customize the select query. This allows me to add filters to show results that are relevant. This functionality doesn't exist for the form view of a model.

Describe the solution you would like.

Adding an extendable ModelView.form_query would add another tool to solve the problems laid out in the documentation for relationship loading. Assuming that it's possible, the default function can remain the same as the existing query to populate a form, with users able to override that property in their own models if they need to.

Describe alternatives you considered

There is ModelView.form_ajax_refs, which allows a more restricted experience to ModelView.list_query. Where this breaks down for me is if I need to filter the select statement based on a property that isn't directly on the class described in the class. ModelView.form_ajax_refs allows me to specify fields on the given relationship. In the docs, the example is the street and zip_code fields on the address relationship. To extend the example, if the street property pointed to an object, and I wanted to filter this relationship based on a property of the street class, I would not be able to do so.

Additional context

There was a related issue that was raised in the past (now closed), where the user had created their own custom query within the update_model call. I think that the use case in this issue would have benefitted from a ModelView.form_query function to override as a more simplified solution.

There have also been two recent posts (here and here) in the Q&A discussion area about wanting to filter relationships in the form view.

lukeclimen avatar Mar 20 '24 21:03 lukeclimen

@aminalaee if you are able to confirm that the proposed changes would be possible in the form view, I can create a PR to address this.

lukeclimen avatar Apr 02 '24 20:04 lukeclimen

I think this makes sense, can you please proceed with a basic implementation and tests to see how it looks and we work on it?

aminalaee avatar Apr 03 '24 07:04 aminalaee