python - 为 Django 应用程序删除了 Postgres 数据库,现在 manage.py 将无法工作

标签 python django postgresql

当我将抽象模型更改为非抽象然后再返回时,我遇到了 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/

相关文章:

python - print 和 pexpect 记录并读取文件

python - 将 South Migrations 添加到第 3 方 Django 应用程序

ruby-on-rails - Rails 4 + Postgres = 模型时间戳中缺少毫秒

ruby-on-rails - 有没有办法在不准备函数的情况下执行带有占位符的查询?

python - 创建与另一个尺寸相同的空数据框?

python - 套接字选择是否返回 EOF?

python - 按所有键的唯一性对字典列表进行排序

django - Ajax ,Django : status 200 but throws error instead of success

django - 处理 Django 用户订阅到期日期

postgresql - docker-compose up 与 docker-compose run,为什么后者似乎没有启动服务?