editable Field in Django Model

In Django, if you want to make a model field non-editable, you can set the editable attribute to False in the model field definition.  If False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True.

Here’s an example of how to use editable=False in a Django model:

from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True, editable=False)
    is_active = models.BooleanField(default=True, editable=False)

    def __str__(self):
        return self.name

In this example:

  • created_at and updated_at are date-time fields that will automatically be set when the object is created or updated, and they are not editable through forms.
  • is_active is a boolean field that is set to True by default and is not editable through forms.

This setup ensures that these fields are not presented in the admin interface or any other form, thereby preventing users from modifying them directly.

References
https://docs.djangoproject.com/en/5.0/ref/models/fields/#editable