Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Cascading Fields #1041

Closed
boolbag opened this issue Sep 3, 2015 · 5 comments
Closed

Request: Cascading Fields #1041

boolbag opened this issue Sep 3, 2015 · 5 comments

Comments

@boolbag
Copy link

boolbag commented Sep 3, 2015

This gist illustrates a common database problem.

  • two activities are defined: swimming and tennis (use the See Other Tables pull-down to inspect them.)
  • For each activity, two accessories are defined: goggles and swimming suit for swimming; racket and shorts for tennis
  • Two events are defined: The Big Swim, related to swimming; Wimbledon, related to tennis

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
Copy link
Member

pawl commented Sep 3, 2015

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

@boolbag
Copy link
Author

boolbag commented Sep 15, 2015

@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
Copy link

Example with AJAX

@petrus-jvrensburg
Copy link
Contributor

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
Copy link
Contributor

mapio commented Nov 5, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants