将Django模型设为只读?

时间:2022-09-11 18:21:43

What it says on the tin. Is there a way to make a Django model read-only?

它在锡上说的是什么。有没有办法让Django模型只读?

By this I mean a Django model in which once records have been created, they can't be edited.

我的意思是一个Django模型,其中一旦创建了记录,就无法编辑它们。

This would be useful for a model that records transaction history.

这对于记录交易历史的模型很有用。

4 个解决方案

#1


10  

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:

您可以覆盖模型的save方法并检查它是否是现有实体,在这种情况下您不会保存任何更改:

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.

因此,在此示例中,您只在实体尚未获得id时保存更改,这仅仅是当它是尚未插入的新实体时。

#2


3  

You can override the save method and not call super if you wanted to. That'd be a fairly easy way of accomplishing this.

您可以覆盖保存方法,如果您愿意,不要调用super。这是实现这一目标的一种相当简单的方法。

# 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.

如果尝试发生删除或更新而不是简单地返回,您可能还应该引发异常。您希望向用户发出正在发生的信号 - 该行为无效。

#3


3  

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

#4


1  

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)

#1


10  

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:

您可以覆盖模型的save方法并检查它是否是现有实体,在这种情况下您不会保存任何更改:

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.

因此,在此示例中,您只在实体尚未获得id时保存更改,这仅仅是当它是尚未插入的新实体时。

#2


3  

You can override the save method and not call super if you wanted to. That'd be a fairly easy way of accomplishing this.

您可以覆盖保存方法,如果您愿意,不要调用super。这是实现这一目标的一种相当简单的方法。

# 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.

如果尝试发生删除或更新而不是简单地返回,您可能还应该引发异常。您希望向用户发出正在发生的信号 - 该行为无效。

#3


3  

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

#4


1  

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)