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
Comments
👍 |
I'm not sure it is very safe to do so. Also, you can easily support it by overriding |
@mrjoes So we can write an app to include in examples? |
Ugh, examples folder will get really huge. Maybe some sort of recipe section on flask-admin.org would be better. |
Is safety really an issue? Not sure I follow. |
Allows creating and distributing link to the admin panel that'll have some non-safe values preselected even if defaults are totally safe. |
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. |
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 URL parameter for season must be
|
Thanks @jmagnusson @crzyhorse & @rachmadaniHaryono Would you mind adding that to one of the existing examples? With a few comments to explain what's happening? |
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() |
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:
The text was updated successfully, but these errors were encountered: