Django - disable model editing

Is there a way, I hope, without breaking the administrator, to disable editing existing model instances at the ORM level?

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

Preferably, the Save As option should work.

+5
source share
2 answers

Overwrite the save function for your model as follows:

class MyModel (models.Model):

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

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

( ), pk, . .

+8

() ( , self.pk) ( )

. , PostgreSQL :

CREATE RULE noupd_myapp_mymodel AS ON UPDATE TO myapp_mymodel
   DO NOTHING;
CREATE RULE nodel_myapp_mymodel AS ON DELETE TO myapp_mymodel
   DO NOTHING;

, - . . , . , , .

EDIT: , delete() , , " " (Queryset.delete(), , admin) () , SQL: https://docs.djangoproject.com/en/dev/topics/db/queries/#deleting-objects

+4

All Articles