我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.apps.apps.get_models()。
def _from_view_models(self): if hasattr(self._view_meta, "from_view_models"): return self._view_meta.from_view_models tables = { model._meta.db_table: model for model in apps.get_models() } from_models = self._view_meta.from_models from_view_models = set() for label in from_models: if "." in label: app_label, model_name = label.split(".") model = apps.get_model(app_label=app_label, model_name=model_name) if issubclass(model, View): from_view_models.add(model) else: model = tables[label] if issubclass(model, View): from_view_models.add(model) setattr(self._view_meta, "from_view_models", from_view_models) return from_view_models
def handle(self, *args, **options): validate = options.get('validate', False) force = options.get('force', False) app_config = apps.get_app_config('postgresviews') tables = { model._meta.db_table: model for model in apps.get_models() } for model in ViewBase.view_models: app_config.validate_view_model(model, tables) if validate: return if force: drop_views() create_views()
def check_all_models(app_configs=None, **kwargs): errors = [] for model in apps.get_models(): if app_configs is None or model._meta.app_config in app_configs: if not inspect.ismethod(model.check): errors.append( Error( "The '%s.check()' class method is " "currently overridden by %r." % ( model.__name__, model.check), hint=None, obj=model, id='models.E020' ) ) else: errors.extend(model.check(**kwargs)) return errors
def _build_kml_sources(self, sources): """ Goes through the given sources and returns a 3-tuple of the application label, module name, and field name of every GeometryField encountered in the sources. If no sources are provided, then all models. """ kml_sources = [] if sources is None: sources = apps.get_models() for source in sources: if isinstance(source, models.base.ModelBase): for field in source._meta.fields: if isinstance(field, GeometryField): kml_sources.append((source._meta.app_label, source._meta.model_name, field.name)) elif isinstance(source, (list, tuple)): if len(source) != 3: raise ValueError('Must specify a 3-tuple of (app_label, module_name, field_name).') kml_sources.append(source) else: raise TypeError('KML Sources must be a model or a 3-tuple.') return kml_sources
def check_all_models(app_configs=None, **kwargs): errors = [] if app_configs is None: models = apps.get_models() else: models = chain.from_iterable(app_config.get_models() for app_config in app_configs) for model in models: if not inspect.ismethod(model.check): errors.append( Error( "The '%s.check()' class method is currently overridden by %r." % (model.__name__, model.check), obj=model, id='models.E020' ) ) else: errors.extend(model.check(**kwargs)) return errors
def handle(self, *args, **options): page_photos = PagePhoto.objects.filter(instance_id=0) if page_photos.exists(): self.stdout.write('Deleting %s PagePhoto without instance' % page_photos.count()) page_photos.delete() page_files = PageFile.objects.filter(instance_id=0) if page_files.exists(): self.stdout.write('Deleting %s PageFiles without instance' % page_files.count()) page_files.delete() simple_photos = SimplePhoto.objects.filter(instance_id=0) if simple_photos.exists(): self.stdout.write('Deleting %s SimplePhoto without instance' % simple_photos.count()) simple_photos.delete() for app in self.get_apps(): for model in apps.get_models(app): if not model._meta.managed: continue self.process_model(app, model) self.stdout.write('Done')
def handle_noargs(self, *args, **options): verbosity = int(options.get('verbosity')) for model in apps.get_models(): if hasattr(model, 'add_missing_placeholders'): if verbosity: self.stdout.write('Adding placeholders for %s "%s"...' % ( model.objects.count(), force_text(model._meta.verbose_name_plural).title(), )) ok = updated = 0 for obj in model.objects.all(): if obj.add_missing_placeholders(): updated += 1 else: ok += 1 if verbosity: self.stdout.write('%s updated, %s OK' % (updated, ok))
def ready(self): for db in settings.DATABASES: name = db['NAME'] db_wrapper_class = import_string(db['ENGINE'] + '.base.DatabaseWrapper') base_model = getattr(db_wrapper_class, 'base_model', None) if base_model: models = apps.get_models() for model in models: if name == router.db_for_read(model): for k, v in base_model.__dict__: if k == 'objects': model_manager = getattr(model, 'objects', None) if model_manager: manager_cls = model_manager.__class__ custom_cls = v.__class__ new_manager = type('AnyBackendCustomManager', (custom_cls, manager_cls), {}) setattr(model, 'objects', new_manager()) else: setattr(model, 'objects', v) elif not k.startswith('__'): setattr(model, k ,v)
def apps_view(request, pk): boite = get_object_or_404(Boite, pk=pk, user=request.user) # update apps data boite.get_apps_dictionary() apps_list = [] enabled_apps = 0 for model in apps.get_models(): if issubclass(model, App): app_instances = model.objects.filter(boite=boite) pk = None enabled = None if app_instances: first_app = app_instances.first() pk = first_app.pk enabled = first_app.enabled enabled_apps += 1 verbose_name = model._meta.verbose_name.title() apps_list.append({'verbose_name':verbose_name[16:], 'pk':pk, 'enabled':enabled, 'app_label': model._meta.app_label, 'instance': app_instances.first()}) return render(request, 'boites/boite_apps.html', {'boite': boite, 'boite_id': boite.id, 'apps': apps_list, 'show_create_button' : len(apps_list) > enabled_apps})
def _get_models(apps, stack): """ If we are in a migration operation, we need to look in that for models. We really only should be injecting ourselves if we find a frame that contains a database_(forwards|backwards) function. Otherwise, we can look in the `apps` object passed in. """ for frame in stack: frame_locals = frame[0].f_locals if frame[3] == 'database_forwards' and all( local in frame_locals for local in ('from_state', 'to_state', 'schema_editor', 'self') ) and isinstance(frame_locals['self'], Operation): # Should this be from_state, or to_state, or should we look in both? from_state = frame_locals['from_state'] to_state = frame_locals['to_state'] models = set() if to_state.apps: models = models.union(to_state.apps.get_models()) if from_state.apps: models = models.union(from_state.apps.get_models()) return models return apps.get_models()