本文整理匯總了Python中rest_framework.routers.SimpleRouter方法的典型用法代碼示例。如果您正苦於以下問題:Python routers.SimpleRouter方法的具體用法?Python routers.SimpleRouter怎麽用?Python routers.SimpleRouter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rest_framework.routers
的用法示例。
在下文中一共展示了routers.SimpleRouter方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: drf_router
# 需要導入模塊: from rest_framework import routers [as 別名]
# 或者: from rest_framework.routers import SimpleRouter [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
示例2: setUp
# 需要導入模塊: from rest_framework import routers [as 別名]
# 或者: from rest_framework.routers import SimpleRouter [as 別名]
def setUp(self):
RedefineViewSet.as_view = viewset_as_view_decorator(RedefineViewSet.as_view)
router = routers.SimpleRouter()
router.register(r'd-view$', RedefineViewSet, base_name='test_base_name')
self.url_patterns = router.urls
url_parser = UrlParser()
self.apis = url_parser.get_apis(url_patterns=self.url_patterns)
self.api_introspector = ApiIntrospector(self.apis)
self.api_introspector.inspect()
示例3: get_viewset_permissions
# 需要導入模塊: from rest_framework import routers [as 別名]
# 或者: from rest_framework.routers import SimpleRouter [as 別名]
def get_viewset_permissions(viewset, model, user, obj, queryset):
"""
Return a list of actions a user can perform on a viewset. We use the SimpleRouter to extract
routes from viewsets. Possible actions per viewset is cached in memory. The next thing we do
is to check permissions on all actions using the AbakusPermission backend.
"""
router = SimpleRouter()
viewset_cls = viewset.__class__
def get_permissions(viewset_cls):
routes = router.get_routes(viewset_cls)
actions = []
for route in routes:
actions += route.mapping.values()
return [action_to_permission(action) for action in actions]
permissions = (
permission_cache[viewset_cls]
if viewset_cls in permission_cache
else permission_cache.setdefault(viewset_cls, get_permissions(viewset_cls))
)
handler = permission_handler(viewset, model)
return handler.permissions_grant(permissions, user, obj, queryset)
示例4: setUp
# 需要導入模塊: from rest_framework import routers [as 別名]
# 或者: from rest_framework.routers import SimpleRouter [as 別名]
def setUp(self):
MockPersonIndex().reindex()
MockPetIndex().reindex()
self.router = SimpleRouter()
class FacetSerializer(HaystackFacetSerializer):
class Meta:
fields = ["firstname", "lastname", "created"]
class ViewSet1(FacetMixin, HaystackViewSet):
index_models = [MockPerson]
serializer_class = Serializer
facet_serializer_class = FacetSerializer
class ViewSet2(MoreLikeThisMixin, HaystackViewSet):
index_models = [MockPerson]
serializer_class = Serializer
class ViewSet3(HaystackViewSet):
index_models = [MockPerson, MockPet]
serializer_class = Serializer
self.view1 = ViewSet1
self.view2 = ViewSet2
self.view3 = ViewSet3
示例5: add_table
# 需要導入模塊: from rest_framework import routers [as 別名]
# 或者: from rest_framework.routers import SimpleRouter [as 別名]
def add_table(table, create_index=False):
if create_index:
mapp = Mapping()
data_mapping = mapp.generate_data_mapping(table)
record_mapping = mapp.generate_record_data_mapping(table)
delete_mapping = mapp.generate_deleted_data_mapping(table)
add_index(table.name, data_mapping)
add_index(table.name + ".", record_mapping)
add_index(table.name + "..", delete_mapping)
data.initialize.add_serializer(table)
viewset = data.initialize.add_viewset(table)
router = SimpleRouter(trailing_slash=False)
router.register(table.name, viewset, base_name=table.name)
urls = router.urls
data.urls.urlpatterns.extend(urls)
data_url_map[table.name] = urls
viewset = record_data.initialize.add_viewset(table)
router = SimpleRouter(trailing_slash=False)
router.register(table.name, viewset, base_name=table.name)
urls = router.urls
record_data.urls.urlpatterns.extend(urls)
record_data_url_map[table.name] = urls
viewset = deleted_data.initialize.add_viewset(table)
router = SimpleRouter(trailing_slash=False)
router.register(table.name, viewset, base_name=table.name)
urls = router.urls
deleted_data.urls.urlpatterns.extend(urls)
deleted_data_url_map[table.name] = urls
# 添加權限
permission, is_new = models.Permission.objects.get_or_create(name=f"{table.name}.read")
permission.alias = f"{table.name}讀權限"
permission.save()
permission, is_new = models.Permission.objects.get_or_create(name=f"{table.name}.write")
permission.alias = f"{table.name}寫權限"
permission.save()
# 刪除表和api