def get_bases_classes(mixins, hook_class, actions):
"""
This function gets the corresponding base classes in order to construct
the viewset class.
A model can specify the allowed operations to it, e.g. update,
list, delete, etc. Then, a viewset specify the allowed methods based on
model's allowed operations by defining the corresponding bases classes.
By default, all methods are allowed.
Apart from this, also configration object can also include custom mixins
classes. Therefore, they are also added to the list of bases classes.
:returns: A tuple of the corresponding base classes.
"""
hook_class = utils.import_object(hook_class) if hook_class\
else view_mixins.HookMixin
mixins = map(utils.LOAD_CLASS, mixins)
bases = (viewsets.GenericViewSet,) if not actions\
else tuple([MIXINS[action] for action in actions]) + (
viewsets.GenericViewSet,)
return (hook_class,) + tuple(mixins) + tuple(bases)
python类GenericViewSet()的实例源码
def initial(self, request, *args, **kwargs):
"""
Call check_method if present and returns parent
"""
if hasattr(self, 'filter_method'):
self.filter_method(request)
return super(GenericViewSet, self).initial(request, args, kwargs)
def test_attrs(self):
self.assertIsInstance(self.view, mixins.CreateModelMixin)
self.assertIsInstance(self.view, mixins.ListModelMixin)
self.assertIsInstance(self.view, mixins.UpdateModelMixin)
self.assertIsInstance(self.view, mixins.RetrieveModelMixin)
self.assertIsInstance(self.view, mixins.DestroyModelMixin)
self.assertIsInstance(self.view, viewsets.GenericViewSet)
self.assertEqual(self.view.permission_classes, (IsAuthenticated,))
self.assertEqual(self.view.serializer_class, CoinSerializer)
self.assertSequenceEqual(
self.view.filter_backends,
(filters.DjangoFilterBackend, BitFilter))
self.assertEqual(self.view.filter_class, CoinFilter)