Django:使用两种不同的模型创建用户帐户

时间:2018-09-06 11:14:59

标签: python django

免责声明:我已经阅读了django-文档,但没有得出结论。

我被交给了我大学四年级的一个项目以继续工作。 在项目中的应用程序中:accounts-显然是要创建用户帐户。

在models.py

from django.contrib import auth
from django.db import models
class User(auth.models.User, auth.models.PermissionsMixin):

    def __str__(self):
        return "@{}".format(self.username)

尽管在forms.py中未使用此模型。 forms.py

from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from django import forms

class UserCreateForm(UserCreationForm):
    class Meta:
        fields = ("first_name", "last_name", "username", "email", "password1", "password2")
        model = get_user_model()

但是这里导入的模型不同于models.py中定义的模型,而是他从from django.contrib.auth

导入了模型

因此,我尝试从models.py中注释掉模型并进行迁移,这会产生错误。

django.db.utils.OperationalError: near ")": syntax error

我使用的是

而不是model = get_user_model

model = User(来自models.py)

甚至这个产量的错误unable to import User

任何人都可以向我解释一下这是什么关系,为什么要同时使用这两种模型?由于不再是大四,因此我没有机会与他联系。

跟踪- Traceback (most recent call last): File "manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "/home/user/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line utility.execute() File "/home/user/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/user/.local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv self.execute(*args, **cmd_options) File "/home/user/.local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute output = self.handle(*args, **options) File "/home/user/.local/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "/home/user/.local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 203, in handle fake_initial=fake_initial, File "/home/user/.local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) File "/home/user/.local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) File "/home/user/.local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration state = migration.apply(state, schema_editor) File "/home/user/.local/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "/home/user/.local/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 150, in database_forwards schema_editor.remove_field(from_model, from_model._meta.get_field(self.name)) File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 318, in remove_field self._remake_table(model, delete_field=field) File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 265, in _remake_table self.quote_name(model._meta.db_table), File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 133, in execute cursor.execute(sql, params) File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute return super().execute(sql, params) File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute return self.cursor.execute(sql, params) File "/home/user/.local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute return self.cursor.execute(sql, params) File "/home/user/.local/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 296, in execute return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: near ")": syntax error

0 个答案:

没有答案