Archiving model data in Django

I am working with a django app that was growing more than I expected and now you need to archive some data so that it doesn't slow down possible requests. My question is what is the best way to archive an existing model? Here's what I thought so far:

1 - Create a legacy model:

class OriginalModel(models.Model):
    ...
    field = models.CharField(etc...)
    ...

class ArchivedModel(OriginalModel):
    pass

      

2. Create a post_save method, so when something is saved in my original model, it will create an archive instance as well.

3 - Create a celery cleanup task on my original model to keep only the relevant data.

I'm not sure, however, if model inheritance will work as I expect. If anyone has any input to this I would be very grateful.

+3


source to share


1 answer


In your data, the code from ArchivedModel

will be in the OriginalModel

SQL table . At the SQL level, there ArchivedModel

will be a table with a single field id

that refers to the table OriginalModel

. Therefore, if you add objects ArchivedModel

to post_save

, they will be duplicated OriginalModel

: -)

You need to create an abstract base class and inherit from it like real models:



class AbstractModel(models.Model):
    ...
    field = models.CharField(etc...)
    ...
    class Meta:
        abstract = True

class OriginalModel(AbstractModel):
    pass

class ArchivedModel(AbstractModel):
    pass

      

In this case OriginalModel

, ArchivedModel

they will have different SQL tables with the same set of fields.

+3


source







All Articles