开发者

Django admin site: prevent fields from being edited?

开发者 https://www.devze.com 2023-01-19 22:12 出处:网络
is it possible to prevent certain fields开发者_运维知识库 to be edited after they\'ve been saved?

is it possible to prevent certain fields开发者_运维知识库 to be edited after they've been saved? They should be editable when the user creates a new item of a certain model but then when they try to open them to edit certain fields are 'blocked'.

thanks


You could override your ModelAdmin's get_readonly_fields to set certain fields readonly:

class MyAdmin(admin.ModelAdmin):

    def get_readonly_fields(self, request, obj=None):
        if obj: # when editing an object
            return ['field1']
        return self.readonly_fields
0

精彩评论

暂无评论...
验证码 换一张
取 消