pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.79k stars 1.57k forks source link

Request: Cascading Fields #1041

Closed boolbag closed 6 years ago

boolbag commented 9 years ago

This gist illustrates a common database problem.

Now, in EventAccessory, we want to make some notes for accessories for each event. Some notes are pre-populated. If you go to edit the accessory for the current Wimbledon record, you will see that all accessories are available --- not just those for tennis, but swimming accessories too.

Nothing surprising there, but this is the request: a feature that makes it easy to filter form field input values based on the record's current fields. For instance, Wimbledon is a tennis event, so in the accessory field we would only see racket and shorts.

From this old issue and this post on stackoverflow, I realize that the feature is not yet implemented. But there is demand, so I'd like to respectfully add it to the request list.

pawl commented 9 years ago

This gist might have the solution: https://gist.github.com/mrjoes/5521548

boolbag commented 9 years ago

@pawl This looks promising, thank you. Sadly, I haven't understood how to use this idea in my small gist, so I can't confirm if that is the solution.

AccessoryAdmin is where I'd like to cascade the accessory field, limiting its values when editing the Wimbledon event (for instance) to accessory names for the Wimbledon activity (tennis): racket, shorts.

Is AccessoryAdmin where Joe's create_form, edit_form, _use_filtered_parent and _get_parent_list would go? And what might _get_parent_list look like?

If I can get it going for this gist, I think I'll be able to apply it to the larger project I'm working on.

Thank you in advance for your kind input.

Forever-Young commented 7 years ago

Example with AJAX

petrus-jvrensburg commented 6 years ago

From the examples:

class UserAdmin(sqla.ModelView):
    # setup create & edit forms so that only 'available' pets can be selected
    def create_form(self):
        return self._use_filtered_parent(
            super(UserAdmin, self).create_form()
        )

    def edit_form(self, obj):
        return self._use_filtered_parent(
            super(UserAdmin, self).edit_form(obj)
        )

    def _use_filtered_parent(self, form):
        form.pets.query_factory = self._get_parent_list
        return form

    def _get_parent_list(self):
        # only show available pets in the form
        return Pet.query.filter_by(available=True).all()
mapio commented 2 years ago

Here you can have an example based on a one-to-many relation https://github.com/mapio/Flask-Admin-Inline-Models-And-Related-Fields