【问题标题】:Dropped Postgres database for django app and now manage.py won't work删除 django 应用程序的 Postgres 数据库,现在 manage.py 将不起作用
【发布时间】:2023-03-07 00:05:01
【问题描述】:

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

【问题讨论】:

    标签: python django postgresql


    【解决方案1】:

    该错误具有误导性。通常它会在您使用 Django 的contenttypes framework 时出现。它发生在 ContentType.objects.get_for_model(MyModel) 在数据库中创建此模型的内容类型记录之前执行时。通常这意味着您正在其中一个模块内执行此代码,分配给类成员(例如使用class-based views 时)等等。

    当 Django 执行其系统检查时,此行正在执行并产生误导性错误。要修复它,您应该避免这样做,或者将调用包装到this answer 中描述的LazyWrapper 中,以便在第一次读取变量时执行代码。像这样:

    # 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
    

    【讨论】:

    • 感谢@YaroslavAdmin,对它进行了排序
    猜你喜欢
    • 2012-11-24
    • 2012-09-21
    • 2013-01-10
    • 1970-01-01
    • 2020-11-21
    • 2021-09-05
    • 2013-07-20
    • 2015-12-23
    • 1970-01-01
    相关资源
    最近更新 更多