Skip to content

Commit 87f93f9

Browse files
authored
Merge pull request #16 from freeworlder/master
Django 1.10 compatibility
2 parents 4021c93 + 95980ec commit 87f93f9

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

json_views/views.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from django.db.models import ManyToManyField
2727
from django.http import HttpResponseNotAllowed, HttpResponse
2828
from django.core.exceptions import ImproperlyConfigured
29+
from itertools import chain
2930

3031
try:
3132
import json
@@ -89,7 +90,19 @@ def serialize_model(self, obj):
8990
tmp = {}
9091

9192
many = [f.name for f in obj._meta.many_to_many]
92-
for field in obj._meta.get_all_field_names():
93+
94+
# class._meta.get_all_field_names() has been deprecated in Django 1.10
95+
# getting all_field_names according to the docs:
96+
# https://docs.djangoproject.com/en/1.10/ref/models/meta/#migrating-from-the-old-api
97+
# fully backward compatible
98+
all_field_names = list(set(chain.from_iterable(
99+
(field.name, field.attname) if hasattr(field, 'attname') else (field.name,)
100+
for field in obj._meta.get_fields()
101+
# For complete backwards compatibility, you may want to exclude
102+
# GenericForeignKey from the results.
103+
if not (field.many_to_one and field.related_model is None)
104+
)))
105+
for field in all_field_names:
93106
if len(many) > 0 and field in many:
94107
many.remove(field)
95108
tmp[field] = getattr(obj, field).all()

0 commit comments

Comments
 (0)