Exemplo n.º 1
0
    def _field_type_declarations(self, field: models.Field):
        type_declarations = list()
        # If the field is relational, check that it is in the `model_pool`, and that
        # it has not already been "visited" (to prevent an infinite cycle).
        if isinstance(
                field,
            (models.ForeignKey, models.OneToOneField, models.ManyToOneRel)):
            if (field.related_model
                    in self.model_pool) and (field.related_model
                                             not in self.visited_models):
                child_queryset_lookups = QuerysetLookups(
                    model=field.related_model,
                    model_pool=self.model_pool,
                    prefixes=self.prefixes + [field.name],
                    visited_models=self.visited_models + [self.model])
                type_declarations += child_queryset_lookups.type_declarations()
        if not isinstance(field, models.ManyToOneRel):
            # This is the base lookup for the field.
            type_declarations.append(
                self._lookup_key([field.name]) + "? : " +
                self._lookup_type(field, None))

            for lookup_str, lookup_cls in field.get_lookups().items():
                type_declarations.append(
                    self._lookup_key([field.name, lookup_str]) + "? : " +
                    self._lookup_type(field, lookup_cls))
        return type_declarations
Exemplo n.º 2
0
from django_filters.rest_framework.backends import DjangoFilterBackend as BaseClass
from django_filters import compat
from django.db.models import Field

LOOKUP_TYPES = Field.get_lookups().keys()


class DjangoFilterBackend(BaseClass):
    """
    Supports field filtering.
    This functionality requires an entry to the REST_FRAMEWORK settings
    dictionary in settings.py:
    'DEFAULT_FILTER_BACKENDS': ('drf_jsonapi.backends.DjangoFilterBackend')
    """
    def get_schema_fields(self, view):
        """
        Retrieve a list of Field objects from a view

        :param self: This object instance
        :param view: A Django View
        :returns: A list of CoreApi Field objects
        """

        filter_class = getattr(view, 'filter_class', None)
        fields = []

        if not filter_class:
            return fields

        for field_name, field in filter_class.base_filters.items():