我确实看过其他主题,但我找不到任何有用的东西或者对我有帮助。我所做的只是做python manage.py inspectdb > models.py
,然后稍微编辑文件,用户模型,然后进行迁移。
现在,当我尝试创建超级用户时,我收到此错误
D:\Programming\Web\blaine county\website>python manage.py createsuperuser --username=andrei --email=andreigames9@gmail.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
Password:
Password (again):
Traceback (most recent call last):
File "D:\Programming\Python\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "D:\Programming\Python\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
return self.cursor.execute(query, args)
File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "D:\Programming\Python\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "D:\Programming\Python\lib\site-packages\MySQLdb\connections.py", line 277, in query
_mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1054, "Unknown column 'last_login' in 'field list'")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "D:\Programming\Python\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "D:\Programming\Python\lib\site-packages\django\core\management\__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Programming\Python\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Programming\Python\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 59, in execute
return super().execute(*args, **options)
File "D:\Programming\Python\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "D:\Programming\Python\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 179, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
File "D:\Programming\Python\lib\site-packages\django\contrib\auth\models.py", line 161, in create_superuser
return self._create_user(username, email, password, **extra_fields)
File "D:\Programming\Python\lib\site-packages\django\contrib\auth\models.py", line 144, in _create_user
user.save(using=self._db)
File "D:\Programming\Python\lib\site-packages\django\contrib\auth\base_user.py", line 73, in save
super().save(*args, **kwargs)
File "D:\Programming\Python\lib\site-packages\django\db\models\base.py", line 729, in save
force_update=force_update, update_fields=update_fields)
File "D:\Programming\Python\lib\site-packages\django\db\models\base.py", line 759, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "D:\Programming\Python\lib\site-packages\django\db\models\base.py", line 842, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "D:\Programming\Python\lib\site-packages\django\db\models\base.py", line 880, in _do_insert
using=using, raw=raw)
File "D:\Programming\Python\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "D:\Programming\Python\lib\site-packages\django\db\models\query.py", line 1125, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "D:\Programming\Python\lib\site-packages\django\db\models\sql\compiler.py", line 1280, in execute_sql
cursor.execute(sql, params)
File "D:\Programming\Python\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "D:\Programming\Python\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "D:\Programming\Python\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "D:\Programming\Python\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "D:\Programming\Python\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "D:\Programming\Python\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "D:\Programming\Python\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
return self.cursor.execute(query, args)
File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "D:\Programming\Python\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "D:\Programming\Python\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "D:\Programming\Python\lib\site-packages\MySQLdb\connections.py", line 277, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1054, "Unknown column 'last_login' in 'field list'")
这是我的models.py https://pastebin.com/5sEY7t5H
问题是我有一个遗留数据库,我试图将应用程序连接到它。
答案 0 :(得分:7)
使用用户模型和旧数据库时,您需要考虑几件事情。
在Django中有四个User
选项构建它,你在这里尝试做的不是它们中的任何一个。
第一个选项是使用默认的User
模型,第二个选项是使用AbstractUser
模型,第三个选项是AbstractBaseUser
,第四个选项是从相关模型链接回来。
您在这里要做的是使用AbstractUser
选项,但这并不是那样的。 AbstractUser
选项使用基本User
模型中的所有字段+模型中定义的其他字段。
AbstractBaseUser
是一个简单的选项,它使用默认User
模型中的三个字段:password
,last_login
,is_active
以及您在其中定义的任何其他字段模特。
要使其正常运行,您需要运行makemigrations
和migrate
,这将创建User
模型,其中包含last_login
等字段以及所有其他字段。 但是你不能这样做,因为你在managed = False
使用旧版数据库。这意味着:
如果为False,则不会创建或删除数据库表操作 为这个模型执行。如果模型代表一个,这很有用 现有表或由其他人创建的数据库视图 装置
这意味着使用旧数据库和managed = False
选项,您无法进行迁移和迁移,这意味着User
模型的任何Django选项都不可能,因为你总会错过一个或另一个领域。
如果你要使用遗留数据库和你自己的用户模型,那么你需要深入研究Django并重写(覆盖)至少身份验证,登录和注销功能,因为他们只是赢得了#t使用您的Users
模型。您不能只复制使用EntityFramework创建的数据库(我假设)并期望它可以与Django一起使用。
你或多或少有三个理智的选择:
Users
模型(您不必删除它,只需将其从AbstractBase
更改为models.Model
)并在Django中设置您自己的模型然后使用遗留数据库仅用于存储数据。managed = True
,删除与User
模型中的Django Users
模型冲突的字段,进行迁移和迁移。