Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - disable model editing

Is there a way, hopefully without breaking admin, to disable editing existing model instances on the ORM level?

I'm not talking about removing 'Save' and 'Save and continue' buttons from templates - there should be no operations that can change the values of a committed instance of a model.

Preferably, the 'Save As' option should work instead.

like image 656
qdot Avatar asked Feb 29 '12 18:02

qdot


1 Answers

Overwrite the save function for your model like so:

class MyModel(models.Model):

    def save(self, *args, **kwargs):

        if self.pk is None:
            super(MyModel, self).save(*args, **kwargs)

This function only call the superclass save function (which actually saves the change) if there is no pk, e.g. the model instance is new.

like image 79
Kevin Audleman Avatar answered Oct 02 '22 23:10

Kevin Audleman