title: Django IntegrityError (1048, "Column 'network_id' cannot be null")
body: <p>Ok, so i searched high and low for this, and both the docs, and almost every page on google tells me my code should be legit.</p>
  
  <p>Let me start with models.py:</p>
  
  <pre><code>class Network(models.Model):
      title = models.CharField(max_length=50)
      description = models.CharField(max_length=500)
      created = models.DateTimeField(auto_now_add=True)
      updated = models.DateTimeField(auto_now=True)
  
      def __unicode__(self):
         return self.id
  
  class NetworkForm(ModelForm):
      class Meta:
          model = Network
  
  class Range(models.Model):
      network = models.ForeignKey(Network)
      range_start = models.GenericIPAddressField(unpack_ipv4=True)
      range_end = models.GenericIPAddressField(unpack_ipv4=True)
      range_title = models.CharField(max_length=50)
      range_description = models.CharField(max_length=300)
  
      def __unicode__(self):
          return self.range_title
  
  class RangeForm(ModelForm):
      class Meta:
          model = Range
          exclude = ('network',)
  </code></pre>
  
  <p>Then my views.py:</p>
  
  <pre><code>@login_required(login_url='/login/')
  def add_network(request):
  
      if request.method == "POST":
          net_form = NetworkForm(request.POST)
          range_formset = RangeForm(request.POST)
          if net_form.is_valid and range_formset.is_valid:
              net_form.save(commit=False)
              range_formset.network = net_form
              range_formset.save()
              net_form.save()
  
          return HttpResponseRedirect('/')
      else:
          net_form = NetworkForm()
          range_form = RangeForm()
          return render_to_response('networks/add.html', {
              "net_form": net_form,
              "range_form": range_form,
          }, context_instance=RequestContext(request))
  </code></pre>
  
  <p>And my template:</p>
  
  <pre><code>{% extends 'base.html' %}
  
  {% block title %}Add Network{% endblock %}
  
  {% block content %}
  &lt;div class="row-fluid"&gt;
      &lt;div class="span7 offset2 well"&gt;
          &lt;h3&gt;Add network:&lt;/h3&gt;
          &lt;hr /&gt;
          &lt;form class="form-horizontal" method="post" action=""&gt;
              {% csrf_token %}
              {{ net_form }}
              &lt;h4&gt;Add a range to the network:&lt;/h4&gt;
              &lt;hr /&gt;
              &lt;div class="form-inline"&gt;
                  {{ range_form }}
              &lt;/div&gt;
              &lt;hr /&gt;
              &lt;button type="submit" class="btn btn-primary btn-large"&gt;Add network&lt;/button&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>{% endblock %}</p>
  
  <p>Now, there most be something obvious i'm missing here, just failing to make the last connection. If somebody could enlighten me that would be great.</p>
  
  <p>PS: In case anybody wondered i validated my model by looking in MySQL Workbench, MySQL Commandline client, and manage.py dbshell and inspectdb, all id columns are present. I even dropped the whole db and recreated it with manage.py.</p>
  
  <p>Its been grinding my gears for about a week now, so time to ask for help. If anybody would be so kind to help me solve this, it would be great!</p>
  
  <p>Here is the traceback:</p>
  
  <pre><code>Environment:
  
  
  Request Method: POST
  Request URL: http://localhost:8000/network/add/
  
  Django Version: 1.5
  Python Version: 2.7.3
  Installed Applications:
  ('django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.sites',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'django.contrib.admin',
   'django_nose',
   'networks')
  Installed Middleware:
  ('django.middleware.common.CommonMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware')
  
  
  Traceback:
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in   get_response
    115.                         response = callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
    25.                 return view_func(request, *args, **kwargs)
  File "/home/alexander/PycharmProjects/NOC/networks/views.py" in add_network
    29.             range_form.save()
  File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in save
    370.                              fail_message, commit, construct=False)
  File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in save_instance
    87.         instance.save()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
    546.                        force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
    650.                 result = manager._insert([self], fields=fields,    return_id=update_pk, using=using, raw=raw)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in _insert
    215.         return insert_query(self.model, objs, fields, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in insert_query
    1673.     return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
    937.             cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
    41.             return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py" in execute
    127.                 six.reraise(utils.IntegrityError,   utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py" in execute
    120.             return self.cursor.execute(query, args)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py" in execute
    174.             self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py" in defaulterrorhandler
    36.     raise errorclass, errorvalue
  
  Exception Type: IntegrityError at /network/add/
  Exception Value: (1048, "Column 'network_id' cannot be null")
  </code></pre>