Skip to content

Fix ManyToMany schema mapping #181

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

Merged
merged 5 commits into from
Jun 24, 2017
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions graphene_django/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from ..utils import get_model_fields
from .models import Film, Reporter


def test_get_model_fields_no_duplication():
reporter_fields = get_model_fields(Reporter)
reporter_name_set = set([field[0] for field in reporter_fields])
assert len(reporter_fields) == len(reporter_name_set)

film_fields = get_model_fields(Film)
film_name_set = set([field[0] for field in film_fields])
assert len(film_fields) == len(film_name_set)
15 changes: 11 additions & 4 deletions graphene_django/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ class LazyList(object):
DJANGO_FILTER_INSTALLED = False


def get_reverse_fields(model):
def get_reverse_fields(model, local_field_names):
for name, attr in model.__dict__.items():
# Don't duplicate any local fields
if name in local_field_names:
continue

# Django =>1.9 uses 'rel', django <1.9 uses 'related'
related = getattr(attr, 'rel', None) or \
getattr(attr, 'related', None)
Expand All @@ -44,15 +48,18 @@ def maybe_queryset(value):


def get_model_fields(model):
reverse_fields = get_reverse_fields(model)
all_fields = [
local_fields = [
(field.name, field)
for field
in sorted(list(model._meta.fields) +
list(model._meta.local_many_to_many))
]

all_fields += list(reverse_fields)
# Make sure we don't duplicate local fields with "reverse" version
local_field_names = [field[0] for field in local_fields]
reverse_fields = get_reverse_fields(model, local_field_names)

all_fields = local_fields + list(reverse_fields)

return all_fields

Expand Down