From f68763b8222e14eb7f4d1113a035baaed1e2d414 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Mon, 11 Jan 2016 16:50:05 +0100 Subject: [PATCH] Fixed many to many relations in Django model --- graphene/contrib/django/converter.py | 1 + graphene/contrib/django/tests/models.py | 5 +++++ graphene/contrib/django/tests/test_schema.py | 2 +- graphene/contrib/django/utils.py | 2 ++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/graphene/contrib/django/converter.py b/graphene/contrib/django/converter.py index ffdcfc5a..6655940a 100644 --- a/graphene/contrib/django/converter.py +++ b/graphene/contrib/django/converter.py @@ -58,6 +58,7 @@ def convert_field_to_float(field): @convert_django_field.register(models.ManyToManyField) @convert_django_field.register(models.ManyToOneRel) +@convert_django_field.register(models.ManyToManyRel) def convert_field_to_list_or_connection(field): from .fields import DjangoModelField, ConnectionOrListField model_field = DjangoModelField(get_related_model(field)) diff --git a/graphene/contrib/django/tests/models.py b/graphene/contrib/django/tests/models.py index a4ff3386..956d70af 100644 --- a/graphene/contrib/django/tests/models.py +++ b/graphene/contrib/django/tests/models.py @@ -7,6 +7,11 @@ class Pet(models.Model): name = models.CharField(max_length=30) +class Film(models.Model): + reporters = models.ManyToManyField('Reporter', + related_name='films') + + class Reporter(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) diff --git a/graphene/contrib/django/tests/test_schema.py b/graphene/contrib/django/tests/test_schema.py index 07a9a84b..b913ace9 100644 --- a/graphene/contrib/django/tests/test_schema.py +++ b/graphene/contrib/django/tests/test_schema.py @@ -29,7 +29,7 @@ def test_should_map_fields_correctly(): model = Reporter assert_equal_lists( ReporterType2._meta.fields_map.keys(), - ['articles', 'first_name', 'last_name', 'email', 'pets', 'id'] + ['articles', 'first_name', 'last_name', 'email', 'pets', 'id', 'films'] ) diff --git a/graphene/contrib/django/utils.py b/graphene/contrib/django/utils.py index b03c2fc8..d2d17aa0 100644 --- a/graphene/contrib/django/utils.py +++ b/graphene/contrib/django/utils.py @@ -35,6 +35,8 @@ def get_reverse_fields(model): yield new_related elif isinstance(related, models.ManyToOneRel): yield related + elif isinstance(related, models.ManyToManyRel) and not related.symmetrical: + yield related class WrappedQueryset(LazyList):