Filtering options will now be read from the destination node's Meta data

This commit is contained in:
Adam Charnock 2015-12-04 18:41:20 +00:00
parent b4b42db73e
commit 3a23c1f940
3 changed files with 18 additions and 3 deletions

View File

@ -10,8 +10,8 @@ class FilterConnectionResolver(BaseQuerySetConnectionResolver):
def __init__(self, node, on=None, filterset_class=None,
fields=None, order_by=None, extra_filter_meta=None):
self.filterset_class = filterset_class
self.fields = fields
self.order_by = order_by
self.fields = fields or node._meta.filter_fields
self.order_by = order_by or node._meta.filter_order_by
self.extra_filter_meta = extra_filter_meta or {}
self._filterset_class = None
super(FilterConnectionResolver, self).__init__(node, on)

View File

@ -2,7 +2,8 @@ from ...core.classtypes.objecttype import ObjectTypeOptions
from ...relay.types import Node
from ...relay.utils import is_node
VALID_ATTRS = ('model', 'only_fields', 'exclude_fields')
VALID_ATTRS = ('model', 'only_fields', 'exclude_fields',
'filter_fields', 'filter_order_by')
class DjangoOptions(ObjectTypeOptions):
@ -13,6 +14,8 @@ class DjangoOptions(ObjectTypeOptions):
self.valid_attrs += VALID_ATTRS
self.only_fields = None
self.exclude_fields = []
self.filter_fields = None
self.filter_order_by = None
def contribute_to_class(self, cls, name):
super(DjangoOptions, self).contribute_to_class(cls, name)

View File

@ -112,6 +112,18 @@ def test_filter_shortcut_filterset_extra_meta():
assert_orderable(field)
def test_filter_filterset_information_on_meta():
class ReporterFilterNode(DjangoNode):
class Meta:
model = Reporter
filter_fields = ['first_name', 'articles']
filter_order_by = True
field = DjangoFilterConnectionField(ReporterFilterNode)
assert_arguments(field, 'firstName', 'articles')
assert_orderable(field)
def test_global_id_field_implicit():
field = DjangoFilterConnectionField(ArticleNode, fields=['id'])
filterset_class = field.resolver_fn.get_filterset_class()