Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove deprecated warnings in 1.8 and 1.9 #78

Merged
merged 1 commit into from
Aug 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions djgeojson/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from six import string_types, iteritems

import django
from django.db.models.base import Model

try:
Expand Down Expand Up @@ -309,8 +310,8 @@ def serialize_queryset(self, queryset):
opts = queryset.model._meta
local_fields = opts.local_fields
many_to_many_fields = opts.many_to_many
reversed_fields = [obj.field for obj in opts.get_all_related_objects()]
reversed_fields += [obj.field for obj in opts.get_all_related_many_to_many_objects()]
reversed_fields = [obj.field for obj in get_all_related_objects(opts)]
reversed_fields += [obj.field for obj in get_all_related_many_to_many_objects(opts)]

# populate each queryset obj as a feature
for obj in queryset:
Expand Down Expand Up @@ -425,3 +426,33 @@ def FeatureToPython(dictobj):
except Exception as e:
# Map to deserializer error
raise DeserializationError(repr(e))


def get_all_related_objects(opts):
"""
Django 1.8 changed meta api, see
https://docs.djangoproject.com/en/1.8/ref/models/meta/#migrating-old-meta-api
https://code.djangoproject.com/ticket/12663
https://github.com/django/django/pull/3848
Initially from Django REST Framework:
https://github.com/tomchristie/django-rest-framework/blob/3.3.2/rest_framework/compat.py
:param opts: Options instance
:return: list of relations except many-to-many ones
"""
if django.VERSION < (1, 8):
return opts.get_all_related_objects()
else:
return [r for r in opts.related_objects if not r.field.many_to_many]


def get_all_related_many_to_many_objects(opts):
"""
Django 1.8 changed meta api, see docstr in get_all_related_objects()

:param opts: Options instance
:return: list of many-to-many relations
"""
if django.VERSION < (1, 8):
return opts.get_all_related_many_to_many_objects()
else:
return [r for r in opts.related_objects if r.field.many_to_many]