Defining Model Fields
Declaring a Field
A field is declared as a class attribute set to a field class instance, such as models.CharField(...). Django uses this to build the matching database column.
Example: Declaring a Field
A field is declared as a class attribute set to a field class instance, such as models.CharField(...). Django uses this to build the matching database column.
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.IntegerField()
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Required Field Arguments
Some fields require arguments to work correctly. CharField always needs max_length, since Django needs to know the column's maximum size at the database level.
Note: max_length is about database storage size, not form validation length.
Warning: Omitting max_length on a CharField raises a system check error before you can even migrate.
Example: Required Field Arguments
Some fields require arguments to work correctly. CharField always needs max_length, since Django needs to know the column's maximum size at the database level.
from django.db import models
class Comment(models.Model):
author_name = models.CharField(max_length=80)
body = models.TextField()
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Optional Field Behavior
blank=True controls whether a field can be left empty in forms, while null=True controls whether the database column itself can store NULL. They solve different problems.
Note: For text-based fields, prefer blank=True with an empty string default rather than null=True, to avoid two ways of representing 'no value'.
Example: Optional Field Behavior
blank=True controls whether a field can be left empty in forms, while null=True controls whether the database column itself can store NULL. They solve different problems.
from django.db import models
class Profile(models.Model):
bio = models.TextField(blank=True, default='')
nickname = models.CharField(max_length=50, blank=True, null=True)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Default Values
The default argument gives a field a starting value whenever a new object is created without explicitly setting that field.
Example: Default Values
The default argument gives a field a starting value whenever a new object is created without explicitly setting that field.
from django.db import models
class Order(models.Model):
quantity = models.IntegerField(default=1)
is_paid = models.BooleanField(default=False)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Forgetting max_length on a CharField, which raises an error at makemigrations time since CharField requires it.
- Using CharField for very long text like an article body instead of TextField, which has no length limit.
- Leaving a field without a default or null=True, then getting an error when migrating a table that already has rows.
- Fields are class attributes on a model, each one an instance of a field class like CharField or IntegerField.
- Every field type maps to a specific column type and validation rule in the database.
- CharField needs max_length; other fields like TextField or IntegerField don't.
- Fields can take options like default, blank, and null to control their behavior.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: