ValueError:使用延迟引用

时间:2018-05-14 06:53:14

标签: python django migration

我正在开发一个新的django项目并坚持迁移。

我想创建一个UserModel,为了做到这一点,我到目前为止做了两件事。 1.我制作了AuthUser模型并在下面的类中设置了一个Meta类。

models.py

class AuthUser(AbstractUser):
    user_type_id = models.PositiveIntegerField(choices=UserTypes.choices())
    user_id = models.PositiveIntegerField()

    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL'

    @property
    def user_type(self):
        return UserTypes(self.user_type_id)

    def original_orm(self):
        if self.user_type.value == UserTypes.raijosha.value:
            return RaijoshaUsers.objects.filter(id=self.user_id).first()
        elif self.user_type.value == UserTypes.shuttennsha.value:
            return Users.objects.filter(id=self.user_id).first()
  1. 在settings.py中,我设置了AUTH_USER_MODEL。

    AUTH_USER_MODEL ='recommended.AuthUser'

  2. 这是错误代码。

    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, recommend, recommend_raijousha, sessions
    Traceback (most recent call last):
      File "manage.py", line 25, in <module>
        execute_from_command_line(sys.argv)
      File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
        utility.execute()
      File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 355, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 283, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 330, in execute
        output = self.handle(*args, **options)
      File "/usr/local/lib/python3.6/dist-packages/django/core/management/commands/migrate.py", line 164, in handle
        pre_migrate_apps = pre_migrate_state.apps
      File "/usr/local/lib/python3.6/dist-packages/django/utils/functional.py", line 35, in __get__
        res = instance.__dict__[self.name] = self.func(instance)
      File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/state.py", line 218, in apps
        return StateApps(self.real_apps, self.models)
      File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/state.py", line 295, in __init__
        raise ValueError("\n".join(error.msg for error in errors))
    ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'recommend.authuser', but app 'recommend' doesn't provide model 'authuser'.
    

    你能帮助我们或给我一个建议。

      Applying admin.0001_initial...Traceback (most recent call last):
      File "manage.py", line 25, in <module>
        execute_from_command_line(sys.argv)
      File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
        utility.execute()
      File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 355, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 283, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 330, in execute
        output = self.handle(*args, **options)
      File "/usr/local/lib/python3.6/dist-packages/django/core/management/commands/migrate.py", line 204, in handle
        fake_initial=fake_initial,
      File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/executor.py", line 115, in migrate
        state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
      File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
        state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
      File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/executor.py", line 244, in apply_migration
        state = migration.apply(state, schema_editor)
      File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/migration.py", line 129, in apply
        operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
      File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/operations/models.py", line 97, in database_forwards
        schema_editor.create_model(model)
      File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/schema.py", line 254, in create_model
        definition, extra_params = self.column_sql(model, field)
      File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/schema.py", line 144, in column_sql
        db_params = field.db_parameters(connection=self.connection)
      File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 994, in db_parameters
        return {"type": self.db_type(connection), "check": self.db_check(connection)}
      File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 991, in db_type
        return self.target_field.rel_db_type(connection=connection)
      File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 909, in target_field
        return self.foreign_related_fields[0]
      File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 653, in foreign_related_fields
        return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
      File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 640, in related_fields
        self._related_fields = self.resolve_related_fields()
      File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 625, in resolve_related_fields
        raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
    ValueError: Related model 'recommend.authuser' cannot be resolved
    

    考虑到建议,我确实删除了迁移文件。 但是上面显示了另一个错误。

7 个答案:

答案 0 :(得分:9)

如果您执行默认的auth应用迁移,然后又在AUTH_USER_MODEL中更改了settings.py,则会发生这种情况。您可以尝试以下操作:

# comment AUTH_USER_MODEL in settings.py so it points to default User model

python manage.py migrate auth zero

# uncomment to be AUTH_USER_MODEL='recommend.AuthUser'

python manage.py migrate auth

答案 1 :(得分:1)

我必须参考 this stack: 才能解决该问题。 我只是按照以下步骤操作:


因为我没有使用 sqlite,所以我在我的 settings.py 中做了新的数据库配置来引用我的 postgres:

我不再使用的旧数据库:

<块引用>

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'leszexpert', 'USER': 'postgres', 'PASSWORD': '', 'HOST': '127.0.0.1', 'PORT': '5432', } }


我想开始使用的新数据库:

<块引用>

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'leszexpert_db', 'USER': 'postgres', 'PASSWORD': '', 'HOST': '127.0.0.1', 'PORT': '5432', } }

如您所见,不同之处在于 DATABASE NAME(在 ENGINE 之后引用)!!!!!!


  1. 我停止了服务器并且之前已经删除了所有现有的相应迁移。
  2. 我关闭了我的 IDE VScode(我从经验中了解到,当某些内容没有生效时,关闭它并重新打开它后,大多数时间最终都会生效)
  3. 当我重新打开代码编辑器时,我取消了我的模型的注释(我之前评论过它们),然后非常重要:此时不要启动或运行您的服务器。因为现在您将要对上面指定的新数据库进行操作,而您知道我们正在尝试解决的问题是,如果您在运行服务器后运行该类型模型的迁移,它将不会创建您的模型...因此暂时不要运行服务器
  4. 改为这样做:先运行迁移,然后再迁移
  5. ET VOILA(法语表达为 That is it):您的扩展用户模型将在您的数据库中创建:

感谢您阅读我的回答

答案 2 :(得分:0)

我删除所有迁移文件和数据库并应用它。

然后我可以迁移。

答案 3 :(得分:0)

对我来说,帮助进行了两次迁移

  1. 创建新表(在新表和旧表之间没有连接,并且没有AUTH_USER_MODEL = 'recommend.authuser'
  2. 将AUTH_USER_MODEL添加到settins.py和其他具有新表的连接

答案 4 :(得分:0)

注意:我正在使用sqlite3作为数据库。

嗨, 我删除了__init__.py文件之外的所有迁移文件,轻松解决了这个问题。并删除db.sqlite3。现在运行以下命令:python manage.py makemigrations,然后运行python manage.py migrate。现在,您必须再次创建超级用户,为此,只需键入以下命令:python manage.py createsuperuser。然后它将提示输入用户名,电子邮件和密码,因此输入您的凭据,所有内容将继续正常运行,我希望这会有所帮助。

Click Here To View Image

答案 5 :(得分:0)

如果您的迁移在本地而不是在服务器上进行,请为服务器执行此操作;

root@abcomp:~/var/www/yourapp$sudo -u postgres psql
#list your databases
postgres=#\l
postgres=#\c your_dbname;
your_db=# delete from django_migrations;
# after pulling the latest working changes
(venv)root@abcomp/var/www/yourapp$./manage.py migrate --fake

答案 6 :(得分:0)

我正在使用 MySQL。

对我来说,解决方案是在 MySQL Workbench 中删除迁移文件夹中的文件(除了 __ init__.py)、DROPCREATE 数据库并运行 python manage.py makemigrations 和 {再次{1}}。

因此,我不得不创建一个新的超级用户,而我的所有数据都丢失了:(