Django admin site: prevent fields from being edited?

我怕爱的太早我们不能终老 提交于 2019-12-22 10:45:40

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/3918453/django-admin-site-prevent-fields-from-being-edited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!