Prepopulate Django (non-Model) Form

后端 未结 4 1993
生来不讨喜
生来不讨喜 2020-12-12 14:59

I\'m trying to prepopulate the data in my django form based on some information, but NOT using ModelForm, so I can\'t just set the instance.

This seems like it shoul

相关标签:
4条回答
  • 2020-12-12 15:21

    For what it's worth, the FormView class-based view way to do this would be to override the FormView's get_initial function. get_initial returns the initial keyword arguments used by get_form_kwargs to instantiate the form.

    Docs:

    • for get_initial, here,
    • for get_form_kwargs, here.

    Sample code:

    from django.views.generic.edit import FormView
    
    class MyFormView(FormView):
    
        def get_initial(self):
            initial = super(MyFormView, self).get_initial()
            # update initial field defaults with custom set default values:
            initial.update({'charfield1': 'foo', 'charfield2': 'bar'})
            return initial
    
    0 讨论(0)
  • 2020-12-12 15:22

    You can use model_to_dict() to convert an instance to a dictionary, and then populate a form with that. Something like this should work:

    from django.forms.models import model_to_dict
    ...
    my_obj = MyModel.objects.get(abc=123)
    form = MyForm(initial=model_to_dict(my_obj))
    

    Note: I'm using django version 1.3

    0 讨论(0)
  • 2020-12-12 15:39

    Try:

    form = MyForm({'charfield1': 'foo', 'charfield2': 'bar'})
    

    The constructor of Form objects can take a dictionary of field values. This creates a bound form, which can be used to validate the data and render the form as HTML with the data displayed. See the forms API documentation for more details.

    Edit:

    For the sake of completeness, if you do not want to bind the form, and you just want to declare initial values for some fields, you can use the following instead:

    form = MyForm(initial={'charfield1': 'foo', 'charfield2': 'bar'})
    

    See the documentation of initial values for details.

    0 讨论(0)
  • 2020-12-12 15:42

    There are two ways of populating a Django form.

    The first is to pass a dictionary as the first argument when you instantiate it (or pass it as the data kwarg, which is the same thing). This is what you do when you want to use POST data to populate and validate the form.

    data_dict = {'charfield1': 'data1', 'charfield2': 'data2', 'choicefield': 3}
    form = MyForm(data_dict)
    

    However, this will trigger validation on the form, so only works if you are actually passing in valid and complete data to begin with - otherwise you will start off with errors.

    The other way to populate a form is to use the initial parameter (documented here). This gives initial values for the form fields, but does not trigger validation. It's therefore suitable if you're not filling in all values, for example.

    form = MyForm(initial=data_dict)
    

    To populate a choicefield via initial, use the pk value.

    0 讨论(0)
提交回复
热议问题