Ejemplo n.º 1
0
    def prepare_indexes(self):
        """
        Checks all available model classes and their respective indexes and creates them.
        """
        for cls in _registered_models.values():
            # Supporting multiple database routing
            if not self.allow_prepare_indexes(cls):
                continue

            collection = self.get_collection(cls._meta.db_storage)
            # Fields from ordering entry
            if cls._meta.ordering:
                self.ensure_index(collection, '_'.join(cls._meta.ordering), [self.index_tuple(f) for f in cls._meta.ordering])

            # Fields with db_index=True
            for name, field in cls._meta.fields.items():
                if field.db_index and not field.unique:
                    self.ensure_index(collection, name, [self.index_tuple(name)])

            # Fields with unique=True
            for name, field in cls._meta.fields.items():
                if field.unique:
                    self.ensure_index(collection, name, [self.index_tuple(name)], unique=True)

            # Indexes
            for fields in (cls._meta.indexes or []):
                self.ensure_index(collection, '_'.join(fields), [self.index_tuple(f) for f in fields])

            # Unique together
            for fields in (cls._meta.unique_together or []):
                self.ensure_index(collection, '_'.join(fields), [self.index_tuple(f) for f in fields], unique=True)
Ejemplo n.º 2
0
def update_content_types(connection, **kwargs):
    for cls in _registered_models.values():
        if issubclass(cls, models.Model):
            ContentType.get_for_model(cls)
Ejemplo n.º 3
0
 def get_model_class_for_collection(self, name):
     for cls in _registered_models.values():
         if cls._meta.db_storage == name:
             return cls
     raise ModelClassNotFound('Model class for collection "%s" not found.'%name)