Make a Django model read-only?

django, django-models

Solution

You can override the model's save method and check whether it's an existing entity, in which case you won't save any changes:

def save(self, *args, **kwargs):
    if self.id is None:
        super(ModelName, self).save(*args, **kwargs)

So in this example you only save the changes when the entity has not got an `id` yet, which is only the case when it's a new entity that hasn't been inserted yet.

Problem

What it says on the tin. Is there a way to make a Django model read-only? By this I mean a Django model in which once records have been created, they can't be edited. This would be useful for a model that records transaction history.

Original source