Skip to content

Commit

Permalink
Fixed django OneToOne inverse relationship conversion. Fix #170
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed May 20, 2016
1 parent 226f81b commit b431bfe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
10 changes: 9 additions & 1 deletion graphene/contrib/django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)


Expand Down
5 changes: 5 additions & 0 deletions graphene/contrib/django/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
11 changes: 10 additions & 1 deletion graphene/contrib/django/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b431bfe

Please sign in to comment.