Lists all indexes that should be created for the Document collection. It includes all the indexes from super- and sub-classes. Note that it will only return the indexes' fields, not the indexes' options
(cls)
| 950 | |
| 951 | @classmethod |
| 952 | def list_indexes(cls): |
| 953 | """Lists all indexes that should be created for the Document collection. |
| 954 | It includes all the indexes from super- and sub-classes. |
| 955 | |
| 956 | Note that it will only return the indexes' fields, not the indexes' options |
| 957 | """ |
| 958 | if cls._meta.get("abstract"): |
| 959 | return [] |
| 960 | |
| 961 | # get all the base classes, subclasses and siblings |
| 962 | classes = [] |
| 963 | |
| 964 | def get_classes(cls): |
| 965 | if cls not in classes and isinstance(cls, TopLevelDocumentMetaclass): |
| 966 | classes.append(cls) |
| 967 | |
| 968 | for base_cls in cls.__bases__: |
| 969 | if ( |
| 970 | isinstance(base_cls, TopLevelDocumentMetaclass) |
| 971 | and base_cls != Document |
| 972 | and not base_cls._meta.get("abstract") |
| 973 | and base_cls._get_collection().full_name |
| 974 | == cls._get_collection().full_name |
| 975 | and base_cls not in classes |
| 976 | ): |
| 977 | classes.append(base_cls) |
| 978 | get_classes(base_cls) |
| 979 | for subclass in cls.__subclasses__(): |
| 980 | if ( |
| 981 | isinstance(base_cls, TopLevelDocumentMetaclass) |
| 982 | and subclass._get_collection().full_name |
| 983 | == cls._get_collection().full_name |
| 984 | and subclass not in classes |
| 985 | ): |
| 986 | classes.append(subclass) |
| 987 | get_classes(subclass) |
| 988 | |
| 989 | get_classes(cls) |
| 990 | |
| 991 | # get the indexes spec for all the gathered classes |
| 992 | def get_indexes_spec(cls): |
| 993 | indexes = [] |
| 994 | |
| 995 | if cls._meta["index_specs"]: |
| 996 | index_spec = cls._meta["index_specs"] |
| 997 | for spec in index_spec: |
| 998 | spec = spec.copy() |
| 999 | fields = spec.pop("fields") |
| 1000 | indexes.append(fields) |
| 1001 | return indexes |
| 1002 | |
| 1003 | indexes = [] |
| 1004 | for klass in classes: |
| 1005 | for index in get_indexes_spec(klass): |
| 1006 | if index not in indexes: |
| 1007 | indexes.append(index) |
| 1008 | |
| 1009 | # finish up by appending { '_id': 1 } and { '_cls': 1 }, if needed |