Fixed ManyToMany relations mapping in Django Models

This commit is contained in:
Syrus Akbary 2015-10-03 18:21:47 -07:00
parent dd799483b0
commit cf09066787
5 changed files with 16 additions and 6 deletions

View File

@ -26,7 +26,11 @@ class DjangoObjectTypeMeta(ObjectTypeMeta):
return
only_fields = cls._meta.only_fields
reverse_fields = tuple(get_reverse_fields(cls._meta.model))
for field in cls._meta.model._meta.fields + reverse_fields:
all_fields = (list(cls._meta.model._meta.local_fields) +
list(reverse_fields) +
list(cls._meta.model._meta.local_many_to_many))
for field in all_fields:
is_not_in_only = only_fields and field.name not in only_fields
is_excluded = field.name in cls._meta.exclude_fields
if is_not_in_only or is_excluded:

View File

@ -2,10 +2,15 @@ from __future__ import absolute_import
from django.db import models
class Pet(models.Model):
name = models.CharField(max_length=30)
class Reporter(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField()
pets = models.ManyToManyField('self')
def __str__(self): # __unicode__ on Python 2
return "%s %s" % (self.first_name, self.last_name)

View File

@ -89,9 +89,10 @@ def test_should_float_convert_float():
def test_should_manytomany_convert_connectionorlist():
field = assert_conversion(models.ManyToManyField, ConnectionOrListField, Article)
assert isinstance(field.field_type, DjangoModelField)
assert field.field_type.model == Article
graphene_type = convert_django_field(Reporter._meta.local_many_to_many[0])
assert isinstance(graphene_type, ConnectionOrListField)
assert isinstance(graphene_type.field_type, DjangoModelField)
assert graphene_type.field_type.model == Reporter
def test_should_manytoone_convert_connectionorlist():

View File

@ -50,7 +50,7 @@ def test_should_map_fields_correctly():
class Meta:
model = Reporter
assert ReporterType2._meta.fields_map.keys(
) == ['articles', 'first_name', 'last_name', 'id', 'email']
) == ['articles', 'first_name', 'last_name', 'email', 'pets', 'id']
def test_should_map_fields():

View File

@ -49,7 +49,7 @@ def test_pseudo_interface():
assert isinstance(object_type, GraphQLInterfaceType)
assert Character._meta.model == Reporter
assert object_type.get_fields().keys() == [
'lastName', 'email', 'id', 'firstName', 'articles']
'articles', 'firstName', 'lastName', 'email', 'pets', 'id']
def test_interface_resolve_type():