本文整理汇总了Python中rest_framework.viewsets.ModelViewSet方法的典型用法代码示例。如果您正苦于以下问题:Python viewsets.ModelViewSet方法的具体用法?Python viewsets.ModelViewSet怎么用?Python viewsets.ModelViewSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.viewsets
的用法示例。
在下文中一共展示了viewsets.ModelViewSet方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_default_serializer
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def test_default_serializer(self):
if StrictVersion(VERSION) >= StrictVersion('3.0.0'):
return
from rest_framework.viewsets import ModelViewSet
from django.http import HttpRequest
from testapp.models import TestModel
class TestViewSet(ModelViewSet):
model = TestModel
view = TestViewSet()
view.request = HttpRequest()
si = SerializerIntrospector(view.get_serializer_class())
self.assertEqual(si.name, 'TestModelSerializer')
示例2: drf_router
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def drf_router():
"""
DRF Router as a lazy object because it needs to import User model which
can't be done until after django.setup()
"""
from django.contrib.auth.models import User
from rest_framework import routers
from rest_framework import serializers
from rest_framework import viewsets
class UserSerializer(serializers.Serializer):
id = serializers.IntegerField(label="ID", read_only=True)
username = serializers.CharField(max_length=200)
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
router = routers.SimpleRouter()
router.register(r"users", UserViewSet)
return router
示例3: view_and_template
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def view_and_template(self, view, template, address_pattern, address_kwargs=None, get_kwargs=None):
if get_kwargs:
response = self.client.get(reverse(address_pattern, kwargs=address_kwargs), get_kwargs, follow=True)
else:
response = self.client.get(reverse(address_pattern, kwargs=address_kwargs), follow=True)
if template:
self.assertTemplateUsed(response, template)
# catch the APIViewsTest for ProjectViewSet that need additional address_kwargs
if issubclass(view, viewsets.ModelViewSet):
self.assertEqual(response.resolver_match.func.__name__, view.as_view(address_kwargs).__name__)
else:
self.assertEqual(response.resolver_match.func.__name__, view.as_view().__name__)
return response
# \param address_pattern the pattern for the reverse lookup that is used to resolve the target address
# \param address_kwargs optional additional kwargs arguments that might be needed for the resolving of the address
# \param get_kwargs optional additional kwargs for a get request that might be needed
# to specify which content is desired
# \param alternate_error_message optional error message that is shown instead of the default error message
示例4: get_tags
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def get_tags(self, operation_keys):
view = self.view
meta = self.view.get_admin_meta()
tags = {"app:{label}".format(label=meta.app_label)}
if self.is_navigation():
tags.add("navigation")
if issubclass(view.__class__, viewsets.ModelViewSet):
tags.add("crud")
view_method = getattr(view, view.action, None)
if view_method:
include_tags = set(getattr(view_method, "include_tags", None) or [])
exclude_tags = set(getattr(view_method, "exclude_tags", None) or [])
tags |= include_tags
tags -= exclude_tags
return [tag for tag in tags if tag not in meta.exclude_tags]
示例5: test_format_api_patterns_url_import_with_routers
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def test_format_api_patterns_url_import_with_routers(self):
class MockApiViewSet(ModelViewSet):
serializer_class = CommentSerializer
model = User
queryset = User.objects.all()
class AnotherMockApiViewSet(ModelViewSet):
serializer_class = CommentSerializer
model = User
queryset = User.objects.all()
router = DefaultRouter()
router.register(r'other_views', MockApiViewSet, base_name='test_base_name')
router.register(r'more_views', AnotherMockApiViewSet, base_name='test_base_name')
urls_app = patterns('', url(r'^', include(router.urls)))
urls = patterns(
'',
url(r'api/', include(urls_app)),
url(r'test/', include(urls_app))
)
apis = self.urlparser.get_apis(urls)
self.assertEqual(
4, sum(api['path'].find('api') != -1 for api in apis))
self.assertEqual(
4, sum(api['path'].find('test') != -1 for api in apis))
示例6: test_exclude_router_api_root
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def test_exclude_router_api_root(self):
class MyViewSet(ModelViewSet):
serializer_class = CommentSerializer
queryset = User.objects.all()
model = User
router = DefaultRouter()
router.register('test', MyViewSet, base_name='test_base_name')
urls_created = len(router.urls)
apis = self.urlparser.get_apis(router.urls)
self.assertEqual(4, urls_created - len(apis))
示例7: is_method_allowed
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def is_method_allowed(self, callback_cls, method_name):
has_attr = hasattr(callback_cls, method_name)
viewset_method = (issubclass(callback_cls, ModelViewSet) and
method_name in VIEWSET_METHODS.get(self.callback.suffix, []))
return has_attr or viewset_method
示例8: test_get_invoked_action_from_class_based_view
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def test_get_invoked_action_from_class_based_view(self):
class UserViewSet(ModelViewSet):
pass
policy = AccessPolicy()
view_instance = UserViewSet()
view_instance.action = "create"
result = policy._get_invoked_action(view_instance)
self.assertEqual(result, "create")
示例9: create
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def create(self, request: Request, *args, **kwargs) -> viewsets.ModelViewSet.create:
"""
Create a new `ScheduledTask` object
"""
return super(ScheduledTaskViewset, self).create(request, *args, **kwargs)
示例10: update
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def update(self, request: Request, *args, **kwargs) -> viewsets.ModelViewSet.update:
"""
Update an existing `ScheduledTask` object
"""
return super(ScheduledTaskViewset, self).update(request, *args, **kwargs)
示例11: run
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def run(self, request: Request, *args, **kwargs) -> viewsets.ModelViewSet.retrieve:
"""
Triggers a given scheduled task now
"""
_object = self.get_object()
_object.publish()
return self.retrieve(request, *args, **kwargs)
示例12: setUp
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def setUp(self):
from testapp.models import TestModel
class TestModelSerializer(ModelSerializer):
class Meta:
model = TestModel
fields = "__all__"
class TestViewSet(AutoPermissionViewSetMixin, ModelViewSet):
queryset = TestModel.objects.all()
serializer_class = TestModelSerializer
permission_type_map = AutoPermissionViewSetMixin.permission_type_map.copy()
permission_type_map["custom_detail"] = "add"
permission_type_map["custom_nodetail"] = "add"
@action(detail=True)
def custom_detail(self, request):
return Response()
@action(detail=False)
def custom_nodetail(self, request):
return Response()
@action(detail=False)
def unknown(self, request):
return Response()
self.model = TestModel
self.vs = TestViewSet
self.req = APIRequestFactory().get("/")
self.req.user = AnonymousUser()
示例13: test_get_base_viewset
# 需要导入模块: from rest_framework import viewsets [as 别名]
# 或者: from rest_framework.viewsets import ModelViewSet [as 别名]
def test_get_base_viewset(self):
class DummyViewSet(ModelViewSet):
pass
class OtherDummyViewSet(ModelViewSet):
pass
class DummyEndpoint(Endpoint):
model = Product
base_viewset = DummyViewSet
class DummyReadOnlyEndpoint(DummyEndpoint):
read_only = True
class OtherDummyReadOnlyEndpoint(Endpoint):
model = Product
read_only = True
base_viewset = OtherDummyViewSet
base_readonly_viewset = DummyViewSet
dummy = DummyEndpoint()
self.assertTrue(issubclass(dummy.get_base_viewset(), DummyViewSet))
dummy = DummyReadOnlyEndpoint()
self.assertTrue(issubclass(dummy.get_base_viewset(), DummyViewSet))
dummy = OtherDummyReadOnlyEndpoint()
self.assertTrue(issubclass(dummy.get_base_viewset(), DummyViewSet))