Skip to content

Commit

Permalink
Merge pull request #74 from momamene/fix-reverse-fields-name
Browse files Browse the repository at this point in the history
#63 Get name of reverse_fields from model.__dict__
  • Loading branch information
syrusakbary committed Mar 16, 2017
2 parents f6034ab + 45542b6 commit eb02f87
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
5 changes: 1 addition & 4 deletions graphene_django/types.py
Expand Up @@ -21,8 +21,7 @@ def construct_fields(options):
exclude_fields = options.exclude_fields

fields = OrderedDict()
for field in _model_fields:
name = field.name
for name, field in _model_fields:
is_not_in_only = only_fields and name not in options.only_fields
is_already_created = name in options.fields
is_excluded = name in exclude_fields or is_already_created
Expand All @@ -34,8 +33,6 @@ def construct_fields(options):
# Or when there is no back reference.
continue
converted = convert_django_field_with_choices(field, options.registry)
if not converted:
continue
fields[name] = converted

return fields
Expand Down
15 changes: 10 additions & 5 deletions graphene_django/utils.py
Expand Up @@ -30,11 +30,11 @@ def get_reverse_fields(model):
# Hack for making it compatible with Django 1.6
new_related = RelatedObject(related.parent_model, related.model, related.field)
new_related.name = name
yield new_related
yield (name, new_related)
elif isinstance(related, models.ManyToOneRel):
yield related
yield (name, related)
elif isinstance(related, models.ManyToManyRel) and not related.symmetrical:
yield related
yield (name, related)


def maybe_queryset(value):
Expand All @@ -45,8 +45,13 @@ def maybe_queryset(value):

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

all_fields += list(reverse_fields)

return all_fields
Expand Down

0 comments on commit eb02f87

Please sign in to comment.