【问题标题】:How to fix a Deprecation Warning in Django 1.9如何修复 Django 1.9 中的弃用警告
【发布时间】:2016-01-25 20:48:22
【问题描述】:

我是 Django 框架的新用户。我目前正在使用 django_rest_framework 构建一个 REST API。在启动我的服务器时,我收到了我不知道如何修复的弃用警告。

RemovedInDjango110Warning: 'get_all_related_objects 是一个已被弃用的非官方 API。您可以将其替换为“get_fields()” 对于 opts.get_all_related_objects() 中的关系

以上是其中的第一个。有谁知道如何解决这个问题。目前我在 API 中的所有内容是使用内置 ModelViewSet 的标准休息调用,我还用我自己的覆盖了默认身份验证和用户系统,所以我不知道为什么我一直在使用 Django 时收到这些警告1.9 从一开始。

我也得到了这个:

RemovedInDjango110Warning:render() 必须使用 dict 调用,而不是 RequestContext

根据我最初的研究,这与模板有关。我没有使用任何模板,所以我不知道为什么会出现这种情况。

谁能帮我解决这些问题?

【问题讨论】:

  • 您使用的 DRF 版本是最新的吗?找到它们的最佳方法是在创建警告的行上放置一个断点,然后查看调用堆栈
  • 我认为您不必担心此警告,DRF 的作者将在下一次更新中修复它们。
  • 感谢您的回复!是的,我正在使用最新的 DRF。我将忽略它们并等待更新中的修复。我只是确保我没有做错任何事。再次感谢

标签: python django rest django-views django-rest-framework


【解决方案1】:

如果有人登陆这里,关于第二次弃用警告:

RemovedInDjango110Warning:render() 必须使用 dict 调用,而不是 RequestContext

这仅记录在Django code

def render(self, context=None, request=None):
    # A deprecation path is required here to cover the following usage:
    # >>> from django.template import Context
    # >>> from django.template.loader import get_template
    # >>> template = get_template('hello.html')
    # >>> template.render(Context({'name': 'world'}))
    # In Django 1.7 get_template() returned a django.template.Template.
    # In Django 1.8 it returns a django.template.backends.django.Template.
    # In Django 1.10 the isinstance checks should be removed. If passing a
    # Context or a RequestContext works by accident, it won't be an issue
    # per se, but it won't be officially supported either.

可以通过从render() 中删除RequestContextContext 的使用并简单地传递字典来轻松修复它。

在 v1.9 中保持原样并不是最好的做法。正如 Django 开发人员所建议的那样,它可能会也可能不会很好地工作。不同之处在于,在 1.9 中我们会收到弃用警告。

【讨论】:

  • 是的,我刚刚从字典中删除了 Context() 并解决了警告日志。
【解决方案2】:

您不必“修复”弃用警告,好吧,只有警告和事情仍然有效。但是,如果您决定更新它们可能会破坏您的应用程序。因此,通常最好将带有警告的部分重写为新接口,如果它在您的代码中,这些警告中会暗示这些部分。如果它在您使用的某个侧库中,那么您可能需要等待库创建者是否会在下一个版本中更新他/她的库。

关于您的特定警告,除非您决定更新到 Django 1.10,否则您的代码应该可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多