Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin site: prevent fields from being edited?

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

like image 779
mαττjαĸøb Avatar asked Oct 12 '10 19:10

mαττjαĸøb


1 Answers

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
like image 122
Bernhard Vallant Avatar answered Nov 15 '22 09:11

Bernhard Vallant