def handle(self, *labels, **options): if not labels: models = apps.get_models() else: models = [] for label in labels: if '.' in label: models.append(apps.get_model(label)) else: models.extend(apps.get_app_config(label).get_models()) nested_models = [ model for model in models if issubclass(model, Nestable) ] if nested_models: for model in nested_models: self.stdout.write('Regenerating {0}.{1} ...'.format( model._meta.app_label, model.__name__, )) with transaction.atomic(): model._tree_manager.rebuild() self.stdout.write('Trees were successfully regenerated.') else: self.stdout.write('No tree was found.')
def populate(self, force=False, app_label=None, model_names=None): """ Populates the slug history. """ if force or self.count() <= 0: cursor = conn.cursor() if app_label is None: models = apps.get_models() app_config = apps.get_app_config(app_label) if model_names is None: models = app_config.get_models() else: models = [app_config.get_model(name) for name in model_names] with transaction.atomic(): for model in models: opts = model._meta try: slug_field = opts.get_field('slug') except FieldDoesNotExist: continue if model == self.model: continue populate_sql = self.statements[ conn.vendor]['populate'].format( history=self.model._meta.db_table, model=opts.db_table, model_pk=opts.pk.column, model_slug=slug_field.column, ) object_type = ContentType.objects.get_for_model(model) cursor.execute(populate_sql, [object_type.pk])
def test_default_models(self): models = list(apps.get_models()) from django.contrib.auth.models import Group, User from django.contrib.contenttypes.models import ContentType from django.contrib.sites.models import Site from yepes.contrib.registry.models import Entry, LongEntry self.assertIn(Group, models) self.assertIn(User, models) self.assertIn(ContentType, models) self.assertIn(Site, models) self.assertIn(Entry, models) self.assertIn(LongEntry, models)
def get_models(app_label=None, model_names=None): """ Returns the models of the given app matching case-insensitive model names. This is very similar to ``django.db.models.get_model()`` function for dynamically loading models. But this function returns multiple models at the same time and raises appropriate errors if any model cannot be found. Args: app_label (str): Label of the app that contains the model. If None is passed, all models of all available apps will be returned. model_names (list): Names of the models to be retrieved. If None is passed, all models in the app will be returned. Returns: The requested model classes. Example: >>> get_models('registry', ['Entry', 'LongEntry']) [<class 'yepes.contrib.registry.models.Entry'>, <class 'yepes.contrib.registry.models.LongEntry'>] Raises: MissingAppError: If no installed app matches the given app label. UnavailableAppError: If app is not currently available. MissingModelError: If one of the requested models cannot be found. """ if app_label is None: return list(apps.get_models()) app_config = apps.get_app_config(app_label) if model_names is None: return list(app_config.get_models()) else: return [app_config.get_model(name) for name in model_names]
def search(self, *args, **kwargs): """ Proxy to queryset's search method for the manager's model and any models that subclass from this manager's model if the model is abstract. """ if getattr(self.model._meta, 'abstract', False): models = [ m for m in apps.get_models() if issubclass(m, self.model) ] parents = reduce(ior, [ set(m._meta.get_parent_list()) for m in models ]) # Strip out any models that are superclasses of models. models = [m for m in models if m not in parents] else: models = [self.model] kwargs['order_results'] = False kwargs['decorate_results'] = True user = kwargs.pop('user', None) customer = kwargs.pop('customer', None) results = [] for model in models: qs = model._default_manager.get_queryset() if hasattr(qs, 'active'): qs = qs.active(user) if hasattr(qs, 'available'): qs = qs.available(user, customer) if hasattr(qs, 'enabled'): qs = qs.enabled(user) if hasattr(qs, 'published'): qs = qs.published(user) results.extend(qs.search(*args, **kwargs)) return sorted(results, key=lambda r: r.search_score, reverse=True)
def _clean_options(cls, opts): selected_models = opts.pop('models', None) if not selected_models: model_list = apps.get_models(include_auto_created=True) else: model_list = [] for model in selected_models: if not isinstance(model, six.string_types): model_list.append(model) elif '.' in model: model = apps.get_model(model) model_list.append(model) else: app_config = apps.get_app_config(model) model_list.extend( app_config.get_models(include_auto_created=True)) use_natural_keys = opts.pop('use_natural_keys', False) migration_kwargs = { 'models': model_list, 'use_natural_primary_keys': use_natural_keys, 'use_natural_foreign_keys': use_natural_keys, 'use_base_manager': opts.pop('use_base_manager', False), } serializer = opts.pop('serializer', None) if isinstance(serializer, six.string_types): serializer = serializers.get_serializer(serializer) if isinstance(serializer, collections.Callable): serializer = serializer(**opts) export_kwargs = { 'serializer': serializer, } return migration_kwargs, export_kwargs