views:

18

answers:

1

Hello, 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  A: 

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
lazerscience
Hi could you please explain your example a bit more? I'm very new to Django. How does your code checks if the document (model instance) being opened is opened for the first time (creation) or successively (edit) ? I need some fields (not all) to be editable when the instance is created and blocked the following times the instance is opened for editing. thanks!
mαττjαĸøb
If a new document is created no `obj` is passed to the method...
lazerscience