[Fixed]-Make a Django model read-only?

13👍

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.

3👍

You can override the save method and not call super if you wanted to. That’d be a fairly easy way of accomplishing this.

# blatantly ripped the save from another answer, since I forgot to save original model
def save(self, *args, **kwargs):
    if self.id is None:
        super(ModelName, self).save(*args, **kwargs)

def delete(self, *args, **kwargs):
    return

You should probably also raise an exception if a delete or update is attempting to occur instead of simply returning. You want to signal the user what is happening – that the behaviour isn’t valid.

2👍

In addition to other solutions: If your main goal is to avoid write access from the admin, you can modify the used admin class so that nobody has an add/change permission:

class HistoryAdmin(admin.ModelAdmin):

    def has_add_permission(self, request):
        return False

    def has_change_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

1👍

If you don’t want an attempt to modify a record to fail silently:

def save(self, *args, **kwargs):
    if self.pk:
        (raise an exception)
    super(YourModel, self).save(*args, **kwargs)


def delete(self, *args, **kwargs):
    (raise an exception)

Leave a comment