python - Django, Extend BaseUserManager, -
i'm trying extend django's baseusermanager
, when create super user, i'm checking if organization
exists (if not create one) , assign user foreignkey
.
the error i'm gettting is:
creating demo data... traceback (most recent call last): file "manage.py", line 10, in <module> 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 null")
i guess it's has models?
accounts
from django.db import models django.contrib.auth.models import abstractbaseuser, baseusermanager 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() < 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
user model
class user(abstractbaseuser): id = models.autofield(primary_key=true) # custom user models must have 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")
organizations
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)
the error occurs here:
user = self.create_user(email, password=password)
this method saves newly created user organization
set none
. move checking , assigning of organization create_superuser()
create_user()
.
Comments
Post a Comment