'How to separate tables based on the model using FSM and FSM-Log?
I'm currently logging status changes to several models in my Django REST API using the django-fsm and django-fsm-log packages. However, all of the logs are stored in a single table. This is gonna end up being a massive table that will take time to run through to do analysis and reporting. To make it easier I'd like to either break out the logging table by each individual model or sort them by the highest level model that's associated. I'll explain what I mean.
I have 3 models:
class Status:
CREATED = 'created'
ACTIVE = 'active'
CANCELLED = 'cancelled'
COMPLETED = 'completed'
STATUS_CHOICES = (
(Status.CREATED, 'Created'),
(Status.ACTIVE, 'Active'),
(Status.CANCELLED, 'Cancelled'),
(Status.COMPLETED, 'Completed'),
)
# model 1
#=========
class Company(models.Model):
name = models.CharField(max_length=255)
# model 2
#=========
class Order(models.Model):
name = models.CharField(max_length=255)
company = models.ForeignKey(Company, related_name='company_order', on_delete=models.CASCADE)
current_status = FSMField(choices=STATUS_CHOICES, default=Status.CREATED)
@fsm_log_by
@fsm_log_description
@transition(field=current_status, source=Status.CREATED, target=Status.ACTIVE)
def order_created_to_active(self, by=None, description=None):
return "order is now active"
@fsm_log_by
@fsm_log_description
@transition(field=current_status, source=Status.ACTIVE, target=Status.COMPLETED)
def order_active_to_completed(self, by=None, description=None):
return "order has been completed"
@fsm_log_by
@fsm_log_description
@transition(field=current_status, source=Status.ACTIVE, target=Status.CANCELLED)
def order_active_to_cancelled(self, by=None, description=None):
return "order has been cancelled"
# model 3
#=========
class Sale(models.Model):
sale_amount = models.DecimalField(max_digits=19, decimal_places=4)
description = models.CharField(max_length=1000)
company = models.ForeignKey(Company, related_name='company_sale', on_delete=models.CASCADE)
current_status = FSMField(choices=STATUS_CHOICES, default=Status.CREATED)
@fsm_log_by
@fsm_log_description
@transition(field=current_status, source=Status.CREATED, target=Status.ACTIVE)
def sale_created_to_active(self, by=None, description=None):
return "sale is now active"
@fsm_log_by
@fsm_log_description
@transition(field=current_status, source=Status.ACTIVE, target=Status.COMPLETED)
def sale_active_to_completed(self, by=None, description=None):
return "sale has been completed"
Now they all get stored in a table "django_fsm_log_statelog" with the content_type_id and object_id to be able to filter the data. However, the more transitions I add the larger that statelog table will become.
NOTE: I'm using a PostgreSQL DB connected to my Django rest API using psycopg2.
So I'd either like to break the statelog table into a statelog table for each model(order, sale) OR for every company created it stores the Order and Sale statelog tables together for that associated company. I'm looking through the docs and can't seem to find a solution to this scenario. Any help would be great. Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|