django admin make a field read-only when modifying obj but required when adding new obj

In admin I would like to disable a field when modifying object, but make it required when adding new object.

Whats the django way to go about this one?

Answers


You can override the admin's get_readonly_fields method:

class MyModelAdmin(admin.ModelAdmin):

    def get_readonly_fields(self, request, obj=None):
        if obj: # editing an existing object
            return self.readonly_fields + ('field1', 'field2')
        return self.readonly_fields

Need Your Help

string manipulation using Directory.GetFiles()

c# string

i am assigning images[] with an array that holds images file names with full path of given Directory.