Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django ValueError:&x27;数据声明中懒洋洋地引用了……';更改型号名称时_Python_Django - Fatal编程技术网

Python Django ValueError:&x27;数据声明中懒洋洋地引用了……';更改型号名称时

Python Django ValueError:&x27;数据声明中懒洋洋地引用了……';更改型号名称时,python,django,Python,Django,这是我创建的第一个模型 class IPAddresses(models.Model): ''' @brief Class for ip addresses. @attrs name Can be company name ''' ip = models.GenericIPAddressField() name = models.CharField(max_length=150, null=True, blank

这是我创建的第一个模型

class IPAddresses(models.Model):
    '''
    @brief      Class for ip addresses.
    @attrs      name        Can be company name
    '''

    ip = models.GenericIPAddressField()
    name = models.CharField(max_length=150, null=True, blank=True)
    active = models.BooleanField(default=True)

    def __unicode__(self):
        return self.ip



class Authentication(models.Model):
    '''
    @brief      Custom Authentication for dashboard
    @attrs      name    can be a name of a person
    '''


    name = models.CharField(max_length=100, null=True, blank=True)
    password = models.CharField(max_length=200, unique=True)
    ip = models.ManyToManyField(IPAddresses, blank=True)
但是,我将'IPAddress'的型号名称更改为'IPAddress',并运行了migrate。这很好,但是我的下一次迁移没有,并且一直收到此值错误:

  Apply all migrations: admin, auth, cache_admin, contenttypes, core, provider, saba_dashboard, sessions
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/deanchristianarmada/Desktop/projects/asian_gaming/radar/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/Users/deanchristianarmada/Desktop/projects/asian_gaming/radar/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/deanchristianarmada/Desktop/projects/asian_gaming/radar/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/deanchristianarmada/Desktop/projects/asian_gaming/radar/lib/python2.7/site-packages/django/core/management/base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "/Users/deanchristianarmada/Desktop/projects/asian_gaming/radar/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 164, in handle
    pre_migrate_apps = pre_migrate_state.apps
  File "/Users/deanchristianarmada/Desktop/projects/asian_gaming/radar/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/deanchristianarmada/Desktop/projects/asian_gaming/radar/lib/python2.7/site-packages/django/db/migrations/state.py", line 176, in apps
    return StateApps(self.real_apps, self.models)
  File "/Users/deanchristianarmada/Desktop/projects/asian_gaming/radar/lib/python2.7/site-packages/django/db/migrations/state.py", line 249, in __init__
    raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field saba_dashboard.Authentication.ip was declared with a lazy reference to 'core.ipaddresses', but app 'core' doesn't provide model 'ipaddresses'.
The field saba_dashboard.Authentication_ip.ipaddresses was declared with a lazy reference to 'core.ipaddresses', but app 'core' doesn't provide model 'ipaddresses'.

解决方案是访问数据库中的django_migrations表,并删除导致此错误的迁移。然后转到您的特定应用程序迁移文件夹并删除特定迁移

如果需要,这是最后一个选项
/manage.py migrate specific_app
不起作用,这是django 1.10版中的一个常见问题我自己也遇到了这样的死锁,不断得到“ValueError:field…是用对“…”的惰性引用声明的,但app“…”不提供模型“…”

在深入pgadmin并删除/恢复一些表之后,我实际上发现,通过添加一个新的迁移,告诉django所引用的模型已经更改,也可以解决这个问题:

    migrations.AlterField(
        model_name='...',
        name='...',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='...', to='<app>.<new-model>'), <===
    ),
migrations.AlterField(
模型名称='…',
名称=“…”,

field=models.ForeignKey(null=True,on_delete=django.db.models.deletation.SET_null,related_name='…',to='.'),这个问题由django 1.11解决(或者至少对我来说是这样)

我的问题是PyCharm的重构变得有点激进,并更改了实际迁移文件中的模型引用。还原了更改的迁移并能够继续


如果这有助于任何人使用Django的更新版本,该版本通常可以处理模型重命名而不会出现问题。

这应该是可以接受的答案,因为它暴露了根本原因:缺少依赖项。
    migrations.AlterField(
        model_name='...',
        name='...',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='...', to='<app>.<new-model>'), <===
    ),