Archiving Django models
cron, django, foreign-keys, python
Solution
So, after looking into this long and hard, I think the best solution for me is to create a 'flat' version of the object, dereferencing any existing objects, and save that in the database.
The reason for this is that my 'BoxOrder' object can change every week (as the customer edits their address, item, cost etc. Keeping track of all these changes is just plain difficult. Plus, I don't need to do anything with the data other than display it to the sites users.
Basically what I am wanting to do is to create a snapshot, and none of the existing tools really are what I want. Having said that, others may have different priorities, so here's a list of useful links:
[1] SO question regarding storing a snapshot/pickling model instances
[2] Django Simple History Docs - stores model state on every create/update/delete
[3] Django Reversion Docs - allows reverting a model instance
For discussion on [2] and [3], see the comments on Serafim's answer
Problem
I'm creating an online order system for selling items on a regular basis (home delivery of vegetable boxes). I have an 'order' model (simplified) as follows: ``` class BoxOrder(models.Model): customer = models.ForeignKey(Customer) frequency = models.IntegerField(choices=((1, "Weekly"), (2, "Fortnightly))) item = models.ForeignKey(Item) payment_method = models.IntegerField(choices=((1, "Online"), (2, "Free))) ``` Now my 'customer' has the ability to change the frequency of the order, or the 'item' (say 'carrots') being sold or even delete the order all together. What I'd like to do is create weekly 'backups' of all orders processed that week, so that I can see a historical graph of all the orders ever sold every week. The problem with just archiving the order into another table/database is that if an item (say I no longer sell carrots) is deleted for some reason, then that archived BoxOrder would become invalid because of the `ForeignKeys` What would be the best solution for creating an archiving system using Django - so that orders for every week in history are viewable in Django admin, and they are 'static' (i.e. independent of whether any other objects are deleted)? I've thought about creating a new 'flat' `BoxOrderArchive` model, then using a cron job to move orders for a given week over, e.g.: ``` class BoxOrderArchive(models.Model): customer_name = models.CharField(max_length=20) frequency = models.IntegerField() item_name = models.CharField() # refers to BoxOrder.item.name item_price = models.DecimalField(max_digits=10, decimal_places=2) # refers to BoxOrder.item.price payment_method = models.IntegerField() ``` But I feel like that might be a lot of extra work. Before I go down that route, it would be great to know if anybody has any other solutions? Thanks