问题
I am a new user of the Django Framework. I am currently building a REST API with the django_rest_framework. When starting my server I am getting deprecation warnings that I have no idea how to fix.
RemovedInDjango110Warning: 'get_all_related_objects is an unofficial API that has been deprecated. You may be able to replace it with 'get_fields()' for relation in opts.get_all_related_objects()
The above is the first of these. Does anyone know how to fix this issue. All I have in my API at the minute is standard rest calls using the built in ModelViewSet and I have also overwritten the default authentication & user system with my own so I have no idea why I'm getting these warnings as I have been using Django 1.9 from the start.
I also got this:
RemovedInDjango110Warning: render() must be called with a dict, not a RequestContext
From my initial research this is related to templates. I am not using any templates so I don't know why this is coming up.
Can anyone help me to fix these issues?
回答1:
You don't have to "fix" Deprecation Warnings as they are, well, only warnings and things still work. However, if you'll decide to update they might break your app. So usually it's a good idea to rewrite the parts with warnings to new interfaces, that are hinted in those warnings if it's in your code. If it's in some side library you use, then you might want to wait if the library creator will update his/her library in the next release.
Regarding your particular warnings, unless you'll decide to update to Django 1.10, your code should work well.
回答2:
In case someone lands here, regarding the second deprecation warning specifically:
RemovedInDjango110Warning: render() must be called with a dict, not a RequestContext
This is only documented in the 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.
It can be easily fixed by removing the use of RequestContext
or Context
from render()
and simply passing a dictionary.
Leaving it as is in v1.9 is not exactly the best thing to do. As Django devs suggest, it may or may not work well. The difference is that in 1.9 we get the deprecation warning.
来源:https://stackoverflow.com/questions/35002061/how-to-fix-a-deprecation-warning-in-django-1-9