Make a Django model read-only?

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.

Answers


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.


Need Your Help

astra as3 panel layout

actionscript-3 yahoo-astra

I'm trying to create a layout not too different from the feed reader sample.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.