我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用django.apps.apps.all_models()。
def generate_root(self, request): from rest_framework.reverse import reverse from rest_framework.response import Response from rest_framework.decorators import api_view index = OrderedDict(( ('Auth', OrderedDict(( ('????????? ?????? ?? ??????/??????', reverse('get_token', request=request)), ('?????????? ??????', reverse('refresh_token', request=request)), ('???????? ??????', reverse('check_token', request=request)), ))), )) project_apps = settings.PROJECT_APPS for app_name in project_apps: index[app_name.capitalize()] = {} models = dict(apps.all_models[app_name]) for name, model in models.items(): model_dict = {} from rest_framework.reverse import reverse_lazy model_dict[name] = reverse_lazy('api:{}-list'.format(name.lower()), request=request) index[app_name.capitalize()].update(model_dict) def root_view(request): return Response(index) root_view.__name__ = self.name root_view.__doc__ = self.description return api_view(['GET'])(root_view)(request)
def register_models(self): from .models import RestModel from rest_framework.routers import SimpleRouter router = SimpleRouter() for app_name in settings.PROJECT_APPS: models = dict(apps.all_models[app_name]).values() for model in models: if RestModel in model.__bases__: router.register(*model._rest_endpoint()) return router