我试图扩展Django的BaseUserManager
,所以当你创建一个超级用户时,我要检查organization
是否存在(如果没有创建)然后使用ForeignKey
为用户分配。
我得到的错误是:
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 be null")
我想这与模特有关吗?
帐户
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() < 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
用户模型
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")
组织
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)
答案 0 :(得分:1)
此处发生错误:
user = self.create_user(email, password=password)
此方法会将organization
设置为None
的新创建用户保存。将组织的检查和分配从create_superuser()
移至create_user()
。