Skip to content
Related Articles

Related Articles

Improve Article

editable=False – Django Built-in Field Validation

  • Last Updated : 13 Feb, 2020

Built-in Field Validations in Django models are the validations that come predefined to all Django fields. Every field comes in with built-in validations from Django validators. One can also add more built-in field validations for applying or removing certain constraints on a particular field. editable=False will make the field disappear from all forms including admin and ModelForm i.e., it can not be edited using any form. The field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation.

Syntax

field_name = models.Field(editable = False)

Django Built-in Field Validation editable=False Explanation

Illustration of editable=False using an Example. Consider a project named geeksforgeeks having an app named geeks.

Refer to the following articles to check how to create a project and an app in Django.

Enter the following code into models.py file of geeks app. We will be using CharField for experimenting for all field options.






from django.db import models
from django.db.models import Model
# Create your models here.
  
class GeeksModel(Model):
    geeks_field = models.CharField(
                    max_length = 200,  
                    default = "GFG is best"
                    editable = False
                    )

After running makemigrations and migrate on Django and rendering the above model, let us try to create an instance from Django admin interface. You can see that field doesn’t appear in admin interface. Hit Save.
editable-django-built-in-validations

Let us check in admin interface if the instance of model is created.
deafult - Django Built-in Field Validation
Therefore, editable=False modifies the field so that it is not visible to admin interface.

Advanced Concepts with default

editable=False is generally used to hide some fields such as some encrypted code or email address verification code etc from admin panel. To use editable in a field you must specify either of following settings:

  • null=True and blank=True, so that your field doesn’t give required error during model save.
  • default=value, this will also set the field to some value so that it doesn’t give suspicious errors to admin user.

More Built-in Field Validations

Field OptionsDescription
NullIf True, Django will store empty values as NULL in the database. Default is False.
BlankIf True, the field is allowed to be blank. Default is False.
db_columnThe name of the database column to use for this field. If this isn’t given, Django will use the field’s name.
DefaultThe default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.
help_textExtra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form.
primary_keyIf True, this field is the primary key for the model.
editableIf False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True.
error_messagesThe error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override.
help_textExtra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form.
verbose_nameA human-readable name for the field. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces.
validatorsA list of validators to run for this field. See the validators documentation for more information.
UniqueIf True, this field must be unique throughout the table.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :