Skip to content

Add metaclass inheritance test #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rest_framework_filters/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def get_subset(cls, params):
if subset_class is not None:
return subset_class

class FilterSubsetMetaclass(FilterSetMetaclass):
class FilterSubsetMetaclass(type(cls)):
def __new__(cls, name, bases, attrs):
new_class = super(FilterSubsetMetaclass, cls).__new__(cls, name, bases, attrs)
new_class.base_filters = OrderedDict([
Expand Down
28 changes: 27 additions & 1 deletion tests/test_filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
from __future__ import unicode_literals

from django.test import TestCase
from django.utils import six

from rest_framework_filters.compat import set_many
from rest_framework_filters.filterset import FilterSetMetaclass
from rest_framework_filters import filters, FilterSet
from django_filters.filters import BaseInFilter

from .testapp.models import (
Note, Post, Person, Tag, BlogPost,
User, Note, Post, Person, Tag, BlogPost,
)

from .testapp.filters import (
Expand Down Expand Up @@ -292,6 +294,30 @@ def test_non_filter_subset(self):
filterset_class = NoteFilterWithRelated.get_subset(['foobar'])
self.assertEqual(len(filterset_class.base_filters), 0)

def test_metaclass_inheritance(self):
# See: https://github.com/philipn/django-rest-framework-filters/issues/132
class SubMetaclass(FilterSetMetaclass):
pass

class SubFilterSet(six.with_metaclass(SubMetaclass, FilterSet)):
pass

class NoteFilter(SubFilterSet):
author = filters.RelatedFilter(UserFilter)

class Meta:
model = Note
fields = ['title', 'content']

# ensure that the class name is useful when debugging
filterset_class = NoteFilter.get_subset(['author', 'content'])
self.assertEqual(filterset_class.__name__, 'NoteFilterSubset')

# ensure that the FilterSet subset only contains the requested fields
self.assertIn('author', filterset_class.base_filters)
self.assertIn('content', filterset_class.base_filters)
self.assertEqual(len(filterset_class.base_filters), 2)


class FilterOverrideTests(TestCase):

Expand Down