question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pre-filling fields in create view

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

9reactions
crzyhorsecommented, 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.

2reactions
jlelongcommented, Jan 22, 2021

For anyone coming here, I had to tweak https://github.com/flask-admin/flask-admin/issues/1051#issuecomment-372010195 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()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Django: CreateView with pre-populated and uneditable fields ...
I would like to do this with an extended version of CreateView which will be able to handle data on pre-populated fields.
Read more >
How to pre-populate a form in CreateView : r/djangolearning
You can pass an initial model to your form, or data as a python dict. There are several approaches to marking fields as...
Read more >
[Solved] Prepopulate fields in partial view create - CodeProject
Hi all, New to MVC development. Struggling to load a partial view for 'Create' with prepopulated field values from Parent View. Basically I...
Read more >
Need some help with preselecting form fields... - Django Forum
I am able to prefill the Product field but cannot figure out how to pass the ... CreateView): model = Listing form_class =...
Read more >
How to Prepopulate Form Fields With Data From Other Tables ...
As you already know, in Jotform Tables, you can add a new tab to view data from other forms you have. You could...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found