当我将抽象模型更改为非抽象然后再返回时,我遇到了 makemigrations 问题——一些 pk 字段丢失了它们的序列生成器。我在主数据库中修复了它,但是迁移生成的数据库没有修复,我不知道如何修复它,所以我想我只是删除数据库,删除迁移并重新开始。所以我这样做了,当我运行 makemigrations 或实际上运行任何 manage.py 命令时,我收到以下错误。我已经将我的 urls.py 文件恢复到最新的提交,因为我认为我可能在那里搞砸了一些东西。
我正在使用 Django 1.8.4、Python 3.4 和 Postgres 9.4.4.0
$ python manage.py help
Traceback (most recent call last):
File "xxx/envs/concil_3.4/lib/python3.4/site-packages/django/core/urlresolvers.py", line 394, in urlconf_module
return self._urlconf_module
AttributeError: 'RegexURLResolver' object has no attribute '_urlconf_module'
最佳答案
该错误具有误导性。通常它会出现,当你使用 Django 的 contenttypes framework 时.当在数据库中创建该模型的内容类型记录之前执行 ContentType.objects.get_for_model(MyModel)
时,就会发生这种情况。通常这意味着您正在其中一个模块内执行此代码,分配给类成员(例如,使用 class-based views 时)等。
当 Django 执行其系统检查时,此行正在执行并产生误导性错误。要修复它,您应该避免做这样的事情,或者将调用包装到 LazyWrapper
中,如 this answer 中所述。在第一次读取变量时执行代码。像这样:
# lazy content type
mymodel_content_type = LazyWrapper(lambda: ContentType.objects.get_for_model(MyModel))
# use
def myview(request):
# ... some code
print(mymodel_content_type) # query to db is executed here on first access and cached for subsequent accesses
# ... some other code
关于python - 为 Django 应用程序删除了 Postgres 数据库,现在 manage.py 将无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32328369/