agiliq / bookrest

Bookrest - The easiest way to add rest API to an arbitrary DB
65 stars 0 forks source link

Greater Filtering Flexibility #2

Closed ztroop closed 1 year ago

ztroop commented 6 years ago

Great library, I've been testing with PostgreSQL 9.6 and it's looking great! I quickly noticed that the default filtering is limited. It uses (SearchFilter,) by default.

I'd like like to suggest that we remove this and allow filtering to only be determined by DEFAULT_FILTER_BACKENDS, as that is already inherited from ModelViewSet.

If we set both variables, search_fields = filter_fields = searchable_field_names, then we can support SearchFilter and django_filters.rest_framework.DjangoFilterBackend interchangeably if configured in settings.py.

I also removed the usage of is_textual_field because it was filtering out models.DateTimeField and models.Integer, which I would also like to expose for query parameter filtering.

@staticmethod
def get_viewset(model_class, serializer_klass):
    searchable_field_names = [
        field.name for field in model_class._meta.get_fields()
    ]

    class ViewSet(viewsets.ModelViewSet):
        serializer_class = serializer_klass
        queryset = model_class.objects.all()
        search_fields = filter_fields = searchable_field_names

    return type("{}ViewSet".format(model_class._meta), (ViewSet,), {})

Let me know your thoughts! :)