title: How to use Django forms' get_initial_for_field() method?
body: <p>I have two models <code>Company</code> and <code>Package</code>, defined as follows:</p>
  
  <pre><code>class Company(models.Model):
      default_package = models.OneToOneField(
          Package,
          blank=True,
          null=True,
          related_name='default_for_%(class)s')
  
  
  class Package(models.Model):
      company = models.ForeignKey('lucy_web.Company')
  </code></pre>
  
  <p>where <code>lucy_web</code> is the name of the app. So there is a many-to-one relationship between <code>Package</code> and <code>Company</code>, and additionally, a one-to-one relationship between a <code>Company</code> and its <code>default_package</code>.</p>
  
  <p>I'm making a <code>ModelForm</code> for the <code>Package</code> object, of which a simplified version is</p>
  
  <pre><code>class PackageForm(forms.ModelForm):
      class Meta:
          model = Package
          fields = [
              'is_default',
          ]
  
      is_default = forms.BooleanField(initial=True, required=False)
  
      def save(self, *args, **kwargs):
          package = super().save(*args, **kwargs)
          if self.cleaned_data['is_default']:
              package.company.default_package = package
              package.company.save()
          return package
  </code></pre>
  
  <p>So when <code>is_default</code> is set to <code>True</code>, the package gets set to its company's default package.</p>
  
  <p>The problem is that this form is used in both a generic <code>CreateView</code> and and <code>UpdateView</code>, and in the <code>UpdateView</code>, the <code>is_default</code> is still set to <code>True</code> even if the package is not the default package. This would make it easy for a user to inadvertently change a package to default when actually they just want to change another field.</p>
  
  <p>I would like to make the <code>initial</code> for <code>is_default</code> such that it is <code>True</code> for unbound forms, and for bound forms, is <code>True</code> only when the <code>package</code> has a <code>default_for_company</code>. I tried to add this to the <code>__init__()</code>, like so:</p>
  
  <pre><code>def __init__(self, *args, **kwargs):
      super().__init__(*args, **kwargs)
      if self.instance and self.instance.default_for_company:
          self.initial['is_default'] = True
  </code></pre>
  
  <p>Unfortunately, however, for a package that is not the default, this leads to a <code>RelatedObjectDoesNotExist</code> error:</p>
  
  <pre><code>File "/Users/kurtpeek/Documents/Dev/lucy/lucy-web/dashboard/forms/packages.py" in __init__
    60.         if self.instance and self.instance.default_for_company:
  
  File "/Users/kurtpeek/Documents/Dev/lucy/lucy-web/venv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py" in __get__
    407.                     self.related.get_accessor_name()
  
  Exception Type: RelatedObjectDoesNotExist at /dashboard/packages/55
  Exception Value: Package has no default_for_company.
  </code></pre>
  
  <p>Also, overriding the <code>__init__()</code> seems a bit 'hacky' and it would seem that the <a href="https://docs.djangoproject.com/en/2.0/ref/forms/api/#django.forms.Form.get_initial_for_field" rel="nofollow noreferrer"><code>get_inital_for_field</code></a> method is designed for this purpose. Is this something I can define for my <code>PackageForm</code> to 'dynamically' get initial values for the fields in this way? (It was unclear to me from the docs how to apply it in this case).</p>