堆栈跟踪:
Creating test database for alias 'default'...
Destroying old test database 'default'...
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Py27_64\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\Py27_64\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Py27_64\lib\site-packages\django\core\management\commands\test.py", line 50, in run_from_argv
super(Command, self).run_from_argv(argv)
File "C:\Py27_64\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Py27_64\lib\site-packages\django\core\management\commands\test.py", line 71, in execute
super(Command, self).execute(*args, **options)
File "C:\Py27_64\lib\site-packages\django\core\management\base.py", line 285, in execute
output = self.handle(*args, **options)
File "C:\Py27_64\lib\site-packages\django\core\management\commands\test.py", line 88, in handle
failures = test_runner.run_tests(test_labels)
File "C:\Py27_64\lib\site-packages\django\test\runner.py", line 145, in run_tests
old_config = self.setup_databases()
File "C:\Py27_64\lib\site-packages\django\test\runner.py", line 107, in setup_databases
return setup_databases(self.verbosity, self.interactive, **kwargs)
File "C:\Py27_64\lib\site-packages\django\test\runner.py", line 279, in setup_databases
verbosity, autoclobber=not interactive)
File "C:\Py27_64\lib\site-packages\django\db\backends\creation.py", line 339, in create_test_db
load_initial_data=False)
File "C:\Py27_64\lib\site-packages\django\core\management\__init__.py", line 159, in call_command
return klass.execute(*args, **defaults)
File "C:\Py27_64\lib\site-packages\django\core\management\base.py", line 285, in execute
output = self.handle(*args, **options)
File "C:\Py27_64\lib\site-packages\django\core\management\base.py", line 415, in handle
return self.handle_noargs(**options)
File "C:\Py27_64\lib\site-packages\django\core\management\commands\syncdb.py", line 107, in handle_noargs
cursor.execute(statement)
File "C:\Py27_64\lib\site-packages\django\db\backends\util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "C:\Py27_64\lib\site-packages\django\db\utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Py27_64\lib\site-packages\django\db\backends\util.py", line 51, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "auth_user" already exists
我的 settings.py 文件:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'myApp',
)
我的怀疑:在models.py 中,由于之前对models.py 实际工作方式的误解,我手动添加了类AuthUser()。
我想要自定义字段/属性,所以我认为这就是这样做的方法。此外,我实际上修改了 Django 生成的 postgres 中的 auth_user 表,以添加自定义字段。
现在,当我尝试运行 ./manage.py test myApp 时,我得到了上面的堆栈跟踪。下面是类 AuthUser():
class AuthUser(models.Model):
password = models.CharField(max_length=128, blank=True)
username = models.CharField(max_length=30, blank=True)
first_name = models.CharField(max_length=30, blank=True)
last_name = models.CharField(max_length=30, blank=True)
is_staff = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
email = models.CharField(max_length=75, blank=True)
uuid = models.Uuid()
role = models.CharField(max_length=20, blank=True)
curricula = models.ManyToManyField(Curricula,
through='CurriculaUserMap',
null=True,
blank=True)
class Meta:
db_table = 'auth_user'
get_latest_by = 'id'
def __unicode__(self):
return self.username
我还没有验证上面的堆栈跟踪是由在 models.py 中定义 class AuthUser() 和 execution 引起的./manage.py test myApp
。我的问题是:
- 如何继续执行 models.py 的当前实现并成功执行
./manage.py test myApp
并继续我的单元测试? - 如何使用自定义字段修复 models.py 中的 AuthUser 类的正确方法?
- 我可以通过将具有一对一关系的 Django 的 User 类扩展到 models.py 中的 CustomUser 类来解决这个问题吗
- 它还支持我在 postgres DB 中创建的与 CurriculaUserMap 的 ManyToManyField 关系吗?
我担心的是,如果我跳起来以“正确的方式”解决这个问题;它几乎会破坏一切。
感谢您的时间!