0

I want a create form where people can select "a template" which prepoulate the form. The template is another model with the same fields. For example i have an AbstractArticle and TemplateArticle and RealArticle with the same structure.

I am trying to use get_initial() to polulate the form but it takes only a dict and it is odd, because the form is quite complex (I can't get it to work with dropdown).

Is there a way to supply a model instance as initial data?

1 Answers1

0

Without looking at your actual code and what you tried I'm a bit lost, but maybe this post answer is what you are looking for. Doing a bit of changes on that answer I can give you this:

def __init__(self, *args, **kwargs):
    initial = super(MyFormView, self).__init__(*args, **kwargs)
    self.temp_mod = kwargs.pop('template_model_id')
    template_model = TemplateModel.objects.get(id=temp_mod)
    # update initial field defaults with custom set default values:
    initial.update(
        {'field1': template_model.data1, 
         'field2': template_model.data2})
    return initial

Not tested

The idea here is get the template model id from the kwargs, get the object and then fill the information in the initial.update to the correspondant fields.

Pol Frances
  • 392
  • 4
  • 17