Open agordon opened 3 years ago
Hello. I think I have solution for your problem. If it is still relevant I can prepare some code
@oleksandr-l5 - any suggestions or ideas would be much appreciated. Thanks!
You can override form.js for adding all form field values into ajax_lookup request https://github.com/oleksandr-l5/fa_override/blob/master/static/fa_override/admin/js/form.js#L19 and get field values in custom ajax loader using request.args https://github.com/oleksandr-l5/fa_override/blob/master/views.py#L33
Hello,
First, thank you for all your work on flask-admin, it is extremely useful and powerful.
I'm using the sqla/ModelView to manage a (simple) table, which has a many-to-many relationship with another table. When editing/creating an item, I'm using
form_ajax_refs
to query items from the other table. It works great.Now I'm looking for a way to dynamically filter the ajax query based on the values of the currently edited item.
To elaborate:
Here is a contrived example of the database using "children" and "toys", and a secondary many-to-many table connecting them (this is all standard SQLAlchemy):
And below is a simple Flask-Admin/SQLA/ModelView class to list/edit/create the "children" table, using defaults for everything, and a custom ajax query to connect 'toys' records to 'children' records:
This works great, but now I'd like to improve it:
When searching/auto-completing "toys" from the ChildView form, I would like to filter by the child's
preferred_color
field, and only show "toys" that match that color. I can't use thefilters: []
part, because that is static and has no notion of what is the currentChild
object and itspreferred_color
value.Is there any way to achieve this kind of dynamic filtering?
Thanks!