Use filter label as argument description. Fixed #16

This commit is contained in:
Syrus Akbary 2016-10-15 18:21:54 -07:00
parent 775221c427
commit 60bcc0beef
2 changed files with 21 additions and 6 deletions

View File

@ -2,7 +2,7 @@ from datetime import datetime
import pytest import pytest
from graphene import Field, ObjectType, Schema from graphene import Field, ObjectType, Schema, Argument, Float
from graphene.relay import Node from graphene.relay import Node
from graphene_django import DjangoObjectType from graphene_django import DjangoObjectType
from graphene_django.forms import (GlobalIDFormField, from graphene_django.forms import (GlobalIDFormField,
@ -46,10 +46,7 @@ class PetNode(DjangoObjectType):
def get_args(field): def get_args(field):
if isinstance(field.args, list): return field.args
return {arg.name: arg for arg in field.args}
else:
return field.args
def assert_arguments(field, *arguments): def assert_arguments(field, *arguments):
@ -252,6 +249,22 @@ def test_global_id_field_explicit():
assert id_filter.field_class == GlobalIDFormField assert id_filter.field_class == GlobalIDFormField
def test_filterset_descriptions():
class ArticleIdFilter(django_filters.FilterSet):
class Meta:
model = Article
fields = ['id']
max_time = django_filters.NumberFilter(method='filter_max_time', label="The maximum time")
field = DjangoFilterConnectionField(ArticleNode, filterset_class=ArticleIdFilter)
max_time = field.args['max_time']
assert isinstance(max_time, Argument)
assert max_time.type == Float
assert max_time.description == 'The maximum time'
def test_global_id_field_relation(): def test_global_id_field_relation():
field = DjangoFilterConnectionField(ArticleNode, fields=['reporter']) field = DjangoFilterConnectionField(ArticleNode, fields=['reporter'])
filterset_class = field.filterset_class filterset_class = field.filterset_class

View File

@ -14,12 +14,14 @@ def get_filtering_args_from_filterset(filterset_class, type):
args = {} args = {}
for name, filter_field in six.iteritems(filterset_class.base_filters): for name, filter_field in six.iteritems(filterset_class.base_filters):
field_type = convert_form_field(filter_field.field) field_type = convert_form_field(filter_field.field).Argument()
field_type.description = filter_field.label
args[name] = field_type args[name] = field_type
# Also add the 'order_by' field # Also add the 'order_by' field
if filterset_class._meta.order_by: if filterset_class._meta.order_by:
args[filterset_class.order_by_field] = String() args[filterset_class.order_by_field] = String()
return args return args