def get_all_models(app_label=None): """Returns all model classes of all apps, including registered abstract ones. The resulting data structure is a dictionary which maps the class names to the model classes. Note that every app must have a ``models.py`` module. This ``models.py`` may be empty, though. :param app_label: the name of the app whose models should be returned :type app_label: unicode :return: all models of all apps :rtype: dict mapping str to ``class`` """ global all_models, abstract_models if app_label: result = dict((model.__name__, model) for model in apps.get_app_config(app_label).get_models()) result.update((model.__name__, model) for model in abstract_models if model._meta.app_label == app_label) return result if all_models is None: abstract_models = frozenset(abstract_models) all_models = dict((model.__name__, model) for model in apps.get_models()) all_models.update((model.__name__, model) for model in abstract_models) return all_models.copy()
def merge_objects(self, alias_objects=[]): from django.contrib.contenttypes.fields import GenericForeignKey from lucterios.framework.signal_and_lock import Signal if not isinstance(alias_objects, list): alias_objects = [alias_objects] primary_class = self.__class__ for alias_object in alias_objects: if not isinstance(alias_object, primary_class): raise TypeError('Only models of same class can be merged') generic_fields = [] for model in apps.get_models(): generic_fields.extend( filter(lambda x: isinstance(x, GenericForeignKey), model.__dict__.values())) blank_local_fields = set([ field.attname for field in self._meta.local_fields if getattr(self, field.attname) in [None, ''] ]) for alias_object in alias_objects: self._merge_fields_object(alias_object) self._merge_genericfield_object(alias_object, generic_fields) self._merge_blankfield_object(alias_object, blank_local_fields) alias_object.delete() self.save() Signal.call_signal("post_merge", self)
def get_all_models(app_label=None): """Returns all model classes of all apps, including registered abstract ones. The resulting data structure is a dictionary which maps the class names to the model classes. Note that every app must have a ``models.py`` module. This ``models.py`` may be empty, though. :param app_label: the name of the app whose models should be returned :type app_label: unicode :return: all models of all apps :rtype: dict mapping str to ``class`` """ global all_models, abstract_models if app_label: result = dict((model.__name__, model) for model in apps.get_app_config(app_label).get_models()) result.update((model.__name__, model) for model in abstract_models if model._meta.app_label == app_label) return result if all_models is None: abstract_models = frozenset(abstract_models) all_models = dict( (model.__name__, model) for model in apps.get_models()) all_models.update((model.__name__, model) for model in abstract_models) return all_models.copy()
def get_models(app_mod=None, include_auto_created=False): """Return the models belonging to an app. Args: app_mod (module, optional): The application module. include_auto_created (bool, optional): Whether to return auto-created models (such as many-to-many models) in the results. Returns: list: The list of modules belonging to the app. """ if apps: # Django >= 1.7 if app_mod is None: return apps.get_models(include_auto_created=include_auto_created) else: app_label = app_mod.__name__.split('.')[-2] try: app_config = apps.get_app_config(app_label) return list(app_config.get_models( include_auto_created=include_auto_created)) except LookupError: return [] else: # Django < 1.7 return _get_models(app_mod, include_auto_created=include_auto_created)
def get_models(app_mod=None, include_auto_created=False): """Return the models belonging to an app. Args: app_mod (module, optional): The application module. include_auto_created (bool, optional): Whether to return auto-created models (such as many-to-many models) in the results. Returns: list: The list of modules belonging to the app. """ if apps: # Django >= 1.7 if app_mod is None: return apps.get_models(include_auto_created=include_auto_created) else: app_label = app_mod.__name__.split('.')[-2] try: app_config = apps.get_app_config(app_label) return [ model for model in app_config.get_models( include_auto_created=include_auto_created) if not model._meta.abstract ] except LookupError: return [] else: # Django < 1.7 return _get_models(app_mod, include_auto_created=include_auto_created)
def _dump_files(self, tar): """ Dump all uploaded media to the archive. """ # Loop through all models and find FileFields for model in apps.get_models(): # Get the name of all file fields in the model field_names = [] for field in model._meta.fields: if isinstance(field, models.FileField): field_names.append(field.name) # If any were found, loop through each row if len(field_names): for row in model.objects.all(): for field_name in field_names: field = getattr(row, field_name) if field: field.open() info = TarInfo(field.name) info.size = field.size tar.addfile(info, field) field.close()
def populate(self): """ Peupler les UUIDs de tous les modèles """ modelset = apps.get_models() for model in modelset: if issubclass(model, FreeUUIDModel): with transaction.atomic(): entries = model.objects.only('pk', 'uuid').all().iterator() for entry in entries: self.add(entry)
def get_fk_list(self, obj): for m in apps.get_models(include_auto_created=True): for f in m._meta.get_all_field_names(): field = m._meta.get_field(f) if (field.many_to_many or field.one_to_many): continue try: relation = field.rel except models.fields.FieldDoesNotExist: continue if relation: if relation.to == obj.__class__: yield(m, f)
def get_models(app_mod=None, include_auto_created=False): """Return the models belonging to an app. Args: app_mod (module, optional): The application module. include_auto_created (bool, optional): Whether to return auto-created models (such as many-to-many models) in the results. Returns: list: The list of modules belonging to the app. """ if apps: # Django >= 1.7 if app_mod is None: return apps.get_models(include_auto_created=include_auto_created) for app_config in apps.get_app_configs(): if app_config.models_module is app_mod: return [ model for model in app_config.get_models( include_auto_created=include_auto_created) if not model._meta.abstract ] return [] else: # Django < 1.7 models = _get_models(app_mod, include_auto_created=include_auto_created) if app_mod is not None: # Avoids a circular import. from django_evolution.utils.apps import get_app_name app_mod_name = get_app_name(app_mod) models = [ model for model in models if model.__module__.startswith(app_mod_name) ] return models
def get_results(self, request=None): """ Searches on the selected models from the request using the search terms from the request. Returns a list of found objects. """ searchable_models = self.get_search_models(request) models = apps.get_models(include_auto_created=False) models = [ model for model in models if issubclass(model, FTSMixin) and model in searchable_models ] querysets = [ self.queryset_for_model(model, request=request) for model in models ] results = chain(*querysets) results = sorted(results, key=attrgetter('fts_rank')) return results
def _dump_referenced_files(self, tar): """ Dump all media files that are reference by a FileField. """ # Loop through all models and find FileFields for model in apps.get_models(): # Get the name of all file fields in the model field_names = [] for field in model._meta.fields: if isinstance(field, models.FileField): field_names.append(field.name) # If any were found, loop through each row if len(field_names): for row in model.objects.all(): for field_name in field_names: field = getattr(row, field_name) if field: self._add_file(tar, field) field.close()
def _dump_referenced_files(self, tar): """ Dump all media files that are reference by a FileField. """ # Loop through all models and find FileFields for model in apps.get_models(): # Get the name of all file fields in the model field_names = [] for field in model._meta.fields: if isinstance(field, models.FileField): field_names.append(field.name) # If any were found, loop through each row if len(field_names): for row in model.objects.all(): for field_name in field_names: field = getattr(row, field_name) if field: self._add_file(tar, field) field.close()
def test_vidyouserbase_registry(self): self.assertIn(TestVidyoMock, apps.get_models()) self.assertIn(TestVidyo1, apps.get_models())
def test_jsonfield_no_warning(self): models_lst = apps.get_models() for model in models_lst: # assert there are no warnings. All checks pass. assert not model.check()