Make a Django model read-only?

前端 未结 4 1688
感情败类
感情败类 2021-01-08 00:21

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 edite

4条回答
  •  -上瘾入骨i
    2021-01-08 01:04

    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)
    

提交回复
热议问题