1👍
One way to do this is to set the field as readonly
if it has the criteria you’re looking for by using a custom form class for your admin class.
Example:
class YourClassAdminForm(forms.ModelForm):
class Meta:
model = YourModel
def __init__(self, *args, **kwargs):
super(YourClassAdminForm, self).__init__(*args, **kwargs)
# Check for your criteria to be `readonly`
if self.instance.whatever:
style = 'border:none;background-color:transparent;color:#666;cursor:default;'
self.fields['position].widget_attrs={'readonly': True, 'style': style}
You can also modify the choices within the same if statement. Django admin doesn’t provide styling for readonly fields within a change list, which is why I’m adding some inline CSS for the widget.
Hope that helps you out.
Source:stackexchange.com