[Django]-Django readonly field only on change, but not when creating

63👍

You need to override get_readonly_fields() method of your admin class.

# admin.py
class TeamAdmin(admin.ModelAdmin)
    ...

    def get_readonly_fields(self, request, obj=None):
        if obj: #This is the case when obj is already created i.e. it's an edit
            return ['players']
        else:
            return []

Leave a comment