partitioned.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from django.db.models.base import ModelBase
  2. from psqlextra.types import PostgresPartitioningMethod
  3. from .base import PostgresModel
  4. from .options import PostgresPartitionedModelOptions
  5. class PostgresPartitionedModelMeta(ModelBase):
  6. """Custom meta class for :see:PostgresPartitionedModel.
  7. This meta class extracts attributes from the inner
  8. `PartitioningMeta` class and copies it onto a `_partitioning_meta`
  9. attribute. This is similar to how Django's `_meta` works.
  10. """
  11. default_method = PostgresPartitioningMethod.RANGE
  12. default_key = []
  13. def __new__(cls, name, bases, attrs, **kwargs):
  14. new_class = super().__new__(cls, name, bases, attrs, **kwargs)
  15. meta_class = attrs.pop("PartitioningMeta", None)
  16. method = getattr(meta_class, "method", None)
  17. key = getattr(meta_class, "key", None)
  18. patitioning_meta = PostgresPartitionedModelOptions(
  19. method=method or cls.default_method, key=key or cls.default_key
  20. )
  21. new_class.add_to_class("_partitioning_meta", patitioning_meta)
  22. return new_class
  23. class PostgresPartitionedModel(PostgresModel, metaclass=PostgresPartitionedModelMeta):
  24. """Base class for taking advantage of PostgreSQL's 11.x native support for
  25. table partitioning."""
  26. class Meta:
  27. abstract = True
  28. base_manager_name = "objects"