title: Specifying initial field values for ModelForm associated with inherited models
body: <p><strong>Question</strong> : What is the recommended way to specify an initial value for fields if one uses model inheritance and each child model needs to have different default values when rendering a <code>ModelForm</code>?</p>
  
  <p>Take for example the following models where <code>CompileCommand</code> and <code>TestCommand</code> both need different initial values when rendered as <code>ModelForm</code>.</p>
  
  <pre><code># ------ models.py
  class ShellCommand(models.Model):
      command   = models.Charfield(_("command"), max_length=100)
      arguments = models.Charfield(_("arguments"), max_length=100)
  
  class CompileCommand(ShellCommand):
      # ... default command should be "make"
  
  class TestCommand(ShellCommand):
      # ... default: command = "make", arguments = "test"
  </code></pre>
  
  <p>I am aware that one can used the <code>initial={...}</code> argument when instantiating the form, however I would rather store the initial values within the context of the model (or at least within the associated ModelForm).</p>
  
  <h3>My current approach</h3>
  
  <p>What I'm doing at the moment is storing an initial value dict within <code>Meta</code>, and checking for it in my views.</p>
  
  <pre><code># ----- forms.py
  class CompileCommandForm(forms.ModelForm):
      class Meta:
          model = CompileCommand
          initial_values = {"command":"make"}
  
  class TestCommandForm(forms.ModelForm):
      class Meta:
          model = TestCommand
          initial_values = {"command":"make", "arguments":"test"}
  
  
  # ------ in views
  FORM_LOOKUP = { "compile": CompileCommandFomr, "test": TestCommandForm }
  CmdForm = FORM_LOOKUP.get(command_type, None)
  # ...
  initial = getattr(CmdForm, "initial_values", {})
  form = CmdForm(initial=initial)
  </code></pre>
  
  <p>This feels too much like a hack. I am eager for a more generic / better way to achieve this. Suggestions appreciated.</p>
  
  <h3>Updated solution (looks promising)</h3>
  
  <p>I now have the following in <code>forms.py</code> which allow me to set <code>Meta.default_initial_values</code> without needing extra boilerplate code in views. Default values are used if user does not specify <code>initial={...}</code> args.</p>
  
  <pre><code>class ModelFormWithDefaults(forms.ModelForm):
      def __init__(self, *args, **kwargs):
          if hasattr(self.Meta, "default_initial_values"):
              kwargs.setdefault("initial", self.Meta.default_initial_values)
          super(ModelFormWithDefaults, self).__init__(*args, **kwargs)
  
  class TestCommandForm(ModelFormWithDefaults):
      class Meta:
          model = TestCommand
          default_initial_values = {"command":"make", "arguments":"test"}
  </code></pre>