[Django]-Django admin: make field editable in add but not edit

66👍

✅

Do not set self.readonly_fields to avoid thread issues. Instead override get_readonly_fields method:

def get_readonly_fields(self, request, obj=None):
    if obj: # obj is not None, so this is an edit
        return ['third_party_id',] # Return a list or tuple of readonly fields' names
    else: # This is an addition
        return []

5👍

The above is helpful (shanyu’s answer using get_readonly_fields), however it does not work properly if used in “StackedInline”. The result is two copies of whatever field is marked readonly, and it is not editable in the “add” instance. See this bug: https://code.djangoproject.com/ticket/15602

Hope this saves someone some searching!

1👍

I am not sure if this is the best way, but you could define your own form for the admin. And custom validate your third_party_id, rejecting if it is already set:

Admin.py

class ProductAdminForm(forms.ModelForm):
    class Meta:
        model = Product

    def clean_third_party_id(self):
        cleaned_data = self.cleaned_data
        third_party_id = cleaned_data['third_party_id']
        id = cleaned_data['id']
        obj = Product.objects.get(id=id)
        if obj.third_party_id != third_party_id:
            raise ValidationError("You cannot edit third_party_id, it must stay as %s" % obj.third_party_id)
         return third_party_id


class ProductAdmin(admin.Admin):
    form = [ProductAdminForm,]

Leave a comment