title: Django, Extend BaseUserManager,
body: <p>I'm trying to extend Django's <code>BaseUserManager</code>, so when you create a super user, I'm checking if an <code>organization</code> exists (if not create one) and then assign the user to it with a <code>ForeignKey</code>.</p>
  
  <p>The error I'm gettting is:</p>
  
  <pre><code>Creating Demo Data...
  Traceback (most recent call last):
    File "manage.py", line 10, in &lt;module&gt;
      execute_from_command_line(sys.argv)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
      utility.execute()
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
      self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
      self.execute(*args, **options.__dict__)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 55, in execute
      return super(Command, self).execute(*args, **options)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
      output = self.handle(*args, **options)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 160, in handle
      self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
    File "/Users/Manos/Projects/devboard/project/accounts/models.py", line 36, in create_superuser
      user = self.create_user(email, password=password)
    File "/Users/Manos/Projects/devboard/project/accounts/models.py", line 20, in create_user
      user.save(using=self._db)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/db/models/base.py", line 589, in save
      force_update=force_update, update_fields=update_fields)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/db/models/base.py", line 617, in save_base
      updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/db/models/base.py", line 698, in _save_table
      result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/db/models/base.py", line 731, in _do_insert
      using=using, raw=raw)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
      return getattr(self.get_queryset(), name)(*args, **kwargs)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/db/models/query.py", line 921, in _insert
      return query.get_compiler(using=using).execute_sql(return_id)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 920, in execute_sql
      cursor.execute(sql, params)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
      return super(CursorDebugWrapper, self).execute(sql, params)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
      return self.cursor.execute(sql, params)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
      six.reraise(dj_exc_type, dj_exc_value, traceback)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
      return self.cursor.execute(sql, params)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 129, in execute
      return self.cursor.execute(query, args)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/pymysql/cursors.py", line 134, in execute
      result = self._query(query)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/pymysql/cursors.py", line 282, in _query
      conn.query(q)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/pymysql/connections.py", line 768, in query
      self._affected_rows = self._read_query_result(unbuffered=unbuffered)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/pymysql/connections.py", line 929, in _read_query_result
      result.read()
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/pymysql/connections.py", line 1125, in read
      first_packet = self.connection._read_packet()
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/pymysql/connections.py", line 893, in _read_packet
      packet.check_error()
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/pymysql/connections.py", line 369, in check_error
      err.raise_mysql_exception(self._data)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/pymysql/err.py", line 120, in raise_mysql_exception
      _check_mysql_exception(errinfo)
    File "/Users/Manos/Projects/devboard/env/lib/python2.7/site-packages/pymysql/err.py", line 112, in _check_mysql_exception
      raise errorclass(errno, errorvalue)
  django.db.utils.IntegrityError: (1048, u"Column 'organization_id' cannot be null")
  </code></pre>
  
  <p>I guess it's something that has do with the models? </p>
  
  <p><strong>Accounts</strong></p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
  from project.organizations.models import Organization
  
  class UserManager(BaseUserManager):
  
      def create_user(self, email, password=None):
          ...
  
      def create_superuser(self, email, password):
  
          print "Creating Demo Data..."
  
          if Organization.objects.filter(name="Demo Inc.").count() &lt; 1:
              organization = Organization.objects.create(name="Demo Inc.")
              organization.save()
              print "Demo data created successfully!"
          else:
              organization = Organization.objects.all().first()
  
          user = self.create_user(email, password=password)
          user.organization = organization
          user.is_active = True
          user.is_admin = True
          user.save(using=self._db)
          return user
  </code></pre>
  
  <p><strong>User model</strong></p>
  
  <pre><code>class User(AbstractBaseUser):
  
      id = models.AutoField(primary_key=True)  # custom User models must have an integer PK
      created = models.DateTimeField(auto_now_add=True)
      modified = models.DateTimeField(auto_now=True)
  
      email = models.EmailField(max_length=255, unique=True, db_index=True)
      full_name = models.CharField(max_length=255)
      organization = models.ForeignKey(Organization, related_name="users")
  </code></pre>
  
  <p><strong>Organizations</strong></p>
  
  <pre><code>class Organization(models.Model):
  
      id = UUIDField(primary_key=True, auto=True, db_index=True)
      created = models.DateTimeField(auto_now_add=True)
      modified = models.DateTimeField(auto_now=True)
  
      name = models.CharField(max_length=200, unique=True)
      website = models.URLField(max_length=200, blank=True)
  </code></pre>