don’t try to access the type until after initialization

this allows for fields in a node (using a DjangoFilterConnectionField) that reference themselves
This commit is contained in:
Tony Angerilli 2016-10-21 00:20:49 -07:00
parent d348ec89c5
commit 3e5ae5a3d1

View File

@ -1,5 +1,7 @@
from collections import OrderedDict
from functools import partial
from graphene.types.argument import to_arguments
from ..fields import DjangoConnectionField
from .utils import get_filtering_args_from_filterset, get_filterset_class
@ -9,19 +11,52 @@ class DjangoFilterConnectionField(DjangoConnectionField):
def __init__(self, type, fields=None, order_by=None,
extra_filter_meta=None, filterset_class=None,
*args, **kwargs):
self._order_by = order_by
self._fields = fields
self._type = type
self._filterset_class = filterset_class
self._extra_filter_meta = extra_filter_meta
self._base_args = None
super(DjangoFilterConnectionField, self).__init__(type, *args, **kwargs)
self.order_by = order_by or type._meta.filter_order_by
self.fields = fields or type._meta.filter_fields
meta = dict(model=type._meta.model,
@property
def node_type(self):
if callable(self._type):
return self._type()
return self._type
@property
def order_by(self):
return self._order_by or self.node_type._meta.filter_order_by
@property
def meta(self):
meta = dict(model=self.node_type._meta.model,
fields=self.fields,
order_by=self.order_by)
if extra_filter_meta:
meta.update(extra_filter_meta)
self.filterset_class = get_filterset_class(filterset_class, **meta)
self.filtering_args = get_filtering_args_from_filterset(self.filterset_class, type)
kwargs.setdefault('args', {})
kwargs['args'].update(self.filtering_args)
super(DjangoFilterConnectionField, self).__init__(type, *args, **kwargs)
if self._extra_filter_meta:
meta.update(self.extra_filter_meta)
return meta
@property
def fields(self):
return self._fields or self.node_type._meta.filter_fields
@property
def args(self):
return to_arguments(self._base_args or OrderedDict(), self.filtering_args)
@args.setter
def args(self, args):
self._base_args = args
@property
def filterset_class(self):
return get_filterset_class(self._filterset_class, **self.meta)
@property
def filtering_args(self):
return get_filtering_args_from_filterset(self.filterset_class, self.node_type)
@staticmethod
def connection_resolver(resolver, connection, default_manager, filterset_class, filtering_args,