Decorators¶
Now the decorator must be used on a non-abstract model class that has not yet built a table in the database. If you must use the decorator on a model class that has previously performed a migrate operation, you need to back up the model’s data, then drop the table, and then import the data after you have created a partitioned table.
-
TimeRangePartitioning
[source]¶ Use this decorator to declare the database table corresponding to the model to be partitioned by time range.
Parameters: - partition_key (str) – Partition field name of DateTimeField.
- options –
Currently supports the following keyword parameters:
- default_period(PeriodType): Default partition period.
- default_interval(int): Default detach partition interval.
- default_attach_tablespace(str): Default tablespace for attached tables.
- default_detach_tablespace(str): Default tablespace for attached tables.
Example
from django.db import models from django.utils import timezone from pg_partitioning.decorators import TimeRangePartitioning @TimeRangePartitioning(partition_key="timestamp") class MyLog(models.Model): name = models.TextField(default="Hello World!") timestamp = models.DateTimeField(default=timezone.now, primary_key=True)
-
ListPartitioning
[source]¶ Use this decorator to declare the database table corresponding to the model to be partitioned by list.
Parameters: partition_key (str) – Partition key name, the type of the key must be one of boolean, text or integer. Example
from django.db import models from django.utils import timezone from pg_partitioning.decorators import ListPartitioning @ListPartitioning(partition_key="category") class MyLog(models.Model): category = models.TextField(default="A") timestamp = models.DateTimeField(default=timezone.now, primary_key=True)
Post-Decoration¶
You can run makemigrations
and migrate
commands to create and apply new migrations.
Once the table has been created, it is not possible to turn a regular table into a partitioned table or vice versa.