Field options help control how data is stored and validated in your database. They make sure your data fits rules you set, like length limits or default values.
Field options (max_length, null, blank, default) in Django
field_name = models.CharField(max_length=100, null=True, blank=True, default='example')
max_length sets the maximum number of characters for text fields.
null=True means the database can store a NULL value for this field.
blank=True means the field can be left empty in forms.
default sets a value automatically if none is given.
name = models.CharField(max_length=50)description = models.TextField(null=True, blank=True)
status = models.CharField(max_length=20, default='pending')
This model defines a product with a name, optional description, and a status that defaults to 'available'. When creating a product without description or status, description is None and status is 'available'.
from django.db import models class Product(models.Model): name = models.CharField(max_length=100) description = models.TextField(null=True, blank=True) status = models.CharField(max_length=20, default='available') # Example usage: product = Product(name='Coffee Mug') print(product.name) # Coffee Mug print(product.description) # None print(product.status) # available
Use null=True only for non-string fields or when you want to store NULL in the database.
blank=True affects form validation, allowing empty input.
Always set max_length for CharField to avoid errors.
max_length limits text size.
null controls database NULL values.
blank controls form input allowance.
default sets a fallback value.