Make a Django model read-only?

前端 未结 4 1661
感情败类
感情败类 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条回答
  • 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)
    
    0 讨论(0)
  • 2021-01-08 01:08

    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.

    0 讨论(0)
  • 2021-01-08 01:12

    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.

    0 讨论(0)
  • 2021-01-08 01:16

    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
    
    0 讨论(0)
提交回复
热议问题