Django - disable model editing

前端 未结 2 746
Happy的楠姐
Happy的楠姐 2021-01-12 15:55

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 a

2条回答
  •  不要未来只要你来
    2021-01-12 16:33

    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.

提交回复
热议问题