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

Pre-filling fields in create view #1051

Closed
jacobsvante opened this issue Sep 14, 2015 · 12 comments
Closed

Pre-filling fields in create view #1051

jacobsvante opened this issue Sep 14, 2015 · 12 comments

Comments

@jacobsvante
Copy link
Contributor

It would be nice to be able to pre-configure the create_view endpoint by passing in GET parameters to it.

So this URL:
http://mysite.dev/admin/productsku/?size=2&variant=3&season=1&season=2&ean_code=123456789
Would produce something like this:
screen shot 2015-09-14 at 13 44 56

@rochacbruno
Copy link
Contributor

👍

@mrjoes
Copy link
Member

mrjoes commented Sep 14, 2015

I'm not sure it is very safe to do so. Also, you can easily support it by overriding get_create_form() and passing values from request.

@rochacbruno
Copy link
Contributor

@mrjoes So we can write an app to include in examples?

@mrjoes
Copy link
Member

mrjoes commented Sep 22, 2015

Ugh, examples folder will get really huge. Maybe some sort of recipe section on flask-admin.org would be better.

@jacobsvante
Copy link
Contributor Author

Is safety really an issue? Not sure I follow.

@mrjoes
Copy link
Member

mrjoes commented Sep 23, 2015

Allows creating and distributing link to the admin panel that'll have some non-safe values preselected even if defaults are totally safe.

@crzyhorse
Copy link

crzyhorse commented Aug 16, 2016

Since it seems that neither a code sample or a recipe section were added for this, and since I struggled with this, I thought I would post my solution.

class MyModelView(ModelView):
     # override create form, not get_create_form as mentioned above. 
     def create_form(self):
         form = super(MyModelView, self).create_form()
         if ('variant') in request.args.keys():
             variant_query = self.session.query(Variant).filter(Variant.id = request.args['variant']).one()
             form.variant.query = [variant_query]
        if ('size') in request.args.keys():
             size_query = self.session.query(Size).filter(Size.id = request.args['size']).one()
             form.size.query = [size_query]
        return form 

Also, I'm using this to create action buttons to create entries for another view, with references to the previous view selected by default. If worried about non-safe values, just validate that the request arguments are in acceptable range before using them.

@rachmadaniHaryono
Copy link

another example

class FilteredImageURLView(CustomModelView):
    """Custom view for Tag model."""

    def create_form(self):
        form = super().create_form()
        ImageUrl = models.ImageURL
        if ('img_id') in request.args.keys():
            img_url_m = self.session.query(ImageUrl).filter(ImageUrl.id == request.args['img_id']).one()  # NOQA
            form.img_url.data = img_url_m
        return form

the code form crzyhorse only limit the selection. this one set the default value on the form.

@GrenderG
Copy link

GrenderG commented Apr 3, 2018

Hi all, although this is not 100% related to this issue, how can I achieve this?

fadmin

@che-alar
Copy link

che-alar commented Apr 12, 2018

@GrenderG URL parameter for season must be &seasons=[1,2]

import json

seasons = request.args.get('seasons')
if seasons:
    seasons = json.loads(seasons)
    seasons_query = self.session.query(Seasons).filter(Seasons.id.in_(seasons)).all()
    form.seasons.data = seasons_query

@petrus-jvrensburg
Copy link
Contributor

Thanks @jmagnusson

@crzyhorse & @rachmadaniHaryono Would you mind adding that to one of the existing examples? With a few comments to explain what's happening?

@jlelong
Copy link

jlelong commented Jan 22, 2021

For anyone coming here, I had to tweak #1051 (comment) a bit to make it work as updating the form data fails

class FilteredImageURLView(CustomModelView):
    """Custom view for Tag model."""

    def create_form(self):
        ImageUrl = models.ImageURL
        if ('img_id') in request.args.keys():
            img_url_m = self.session.query(ImageUrl).filter(ImageUrl.id == request.args['img_id']).one()  # NOQA
            return super(imageURL).create_form(image_url_m)
        return super(imageURL).create_form()

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

9 participants