Fixed django OneToOne inverse relationship conversion. Fix #170

This commit is contained in:
Syrus Akbary 2016-05-19 21:42:20 -07:00
parent 226f81bda1
commit b431bfe477
3 changed files with 24 additions and 2 deletions

View File

@ -80,9 +80,15 @@ def convert_date_to_string(field):
return DateTime(description=field.help_text)
@convert_django_field.register(models.OneToOneRel)
def convert_onetoone_field_to_djangomodel(field):
from .fields import DjangoModelField
return DjangoModelField(get_related_model(field))
@convert_django_field.register(models.ManyToManyField)
@convert_django_field.register(models.ManyToOneRel)
@convert_django_field.register(models.ManyToManyRel)
@convert_django_field.register(models.ManyToOneRel)
def convert_field_to_list_or_connection(field):
from .fields import DjangoModelField, ConnectionOrListField
model_field = DjangoModelField(get_related_model(field))
@ -94,6 +100,8 @@ def convert_field_to_list_or_connection(field):
def convert_relatedfield_to_djangomodel(field):
from .fields import DjangoModelField, ConnectionOrListField
model_field = DjangoModelField(field.model)
if isinstance(field.field, models.OneToOneField):
return model_field
return ConnectionOrListField(model_field)

View File

@ -7,6 +7,11 @@ class Pet(models.Model):
name = models.CharField(max_length=30)
class FilmDetails(models.Model):
location = models.CharField(max_length=30)
film = models.OneToOneField('Film', related_name='details')
class Film(models.Model):
reporters = models.ManyToManyField('Reporter',
related_name='films')

View File

@ -9,7 +9,7 @@ from ..compat import (ArrayField, HStoreField, JSONField, MissingType,
RangeField)
from ..converter import convert_django_field, convert_django_field_with_choices
from ..fields import ConnectionOrListField, DjangoModelField
from .models import Article, Reporter
from .models import Article, Reporter, Film, FilmDetails
def assert_conversion(django_field, graphene_field, *args, **kwargs):
@ -138,6 +138,15 @@ def test_should_manytoone_convert_connectionorlist():
assert graphene_type.type.model == Article
def test_should_onetoone_reverse_convert_model():
# Django 1.9 uses 'rel', <1.9 uses 'related
related = getattr(Film.details, 'rel', None) or \
getattr(Film.details, 'related')
graphene_type = convert_django_field(related)
assert isinstance(graphene_type, DjangoModelField)
assert graphene_type.model == FilmDetails
def test_should_onetoone_convert_model():
field = assert_conversion(models.OneToOneField, DjangoModelField, Article)
assert field.type.model == Article