Welcome back to CodeYourCraft! Today, we're diving deep into Django's Model Fields. These fields are like the building blocks of your Django applications, helping you structure your data effectively. Let's get started!
π‘ Pro Tip: Model fields define the type and characteristics of data that a Django model can store.
In Django, we use several field types to create models. Here are some of the most common ones:
π― Key Concept: CharField is used to store character strings.
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)In the example above, we've created a model named MyModel with a single CharField called name. The max_length attribute specifies the maximum number of characters the field can hold.
max_length to specify the maximum length of the string.blank=True if the field can be empty.null=True if the field can contain no value.from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)π― Key Concept: IntegerField is used to store integer values.
from django.db import models
class MyModel(models.Model):
age = models.IntegerField()In the example above, we've created an IntegerField named age. By default, IntegerField allows null values and can be left blank.
blank=True if the field can be empty.null=True if the field can contain no value.default=0.from django.db import models
class MyModel(models.Model):
age = models.IntegerField(default=0, blank=True, null=True)What is the purpose of the CharField in Django?
π― Key Concept: FloatField is used to store floating-point numbers.
from django.db import models
class MyModel(models.Model):
weight = models.FloatField()In the example above, we've created a FloatField named weight. By default, FloatField allows null values and can be left blank.
blank=True if the field can be empty.null=True if the field can contain no value.default=0.0.from django.db import models
class MyModel(models.Model):
weight = models.FloatField(default=0.0, blank=True, null=True)π― Key Concept: BooleanField is used to store Boolean (True/False) values.
from django.db import models
class MyModel(models.Model):
is_active = models.BooleanField(default=True)In the example above, we've created a BooleanField named is_active. By default, BooleanField sets the value to True.
default=True if you want the default value to be True.default=False if you want the default value to be False.from django.db import models
class MyModel(models.Model):
is_active = models.BooleanField(default=False)π― Key Concept: DateField, TimeField, and DateTimeField are used to store date, time, and datetime values respectively.
from django.db import models
class MyModel(models.Model):
creation_date = models.DateField(auto_now_add=True)
last_update = models.DateTimeField(auto_now=True)In the example above, we've created a model named MyModel with two fields:
creation_date: A DateField that automatically adds the current date when a new object is created.last_update: A DateTimeField that automatically updates the current date and time each time the object is saved.auto_now_add=True for DateField and DateTimeField to automatically add the current date when a new object is created.auto_now=True for DateTimeField to automatically update the current date and time each time the object is saved.blank=True if the field can be empty.null=True if the field can contain no value.from django.db import models
class MyModel(models.Model):
creation_date = models.DateField(auto_now_add=True, blank=True, null=True)
last_update = models.DateTimeField(auto_now=True, blank=True, null=True)π― Key Concept: ForeignKey and ManyToManyField help establish relationships between models.
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
class BookReview(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
rating = models.IntegerField()
review = models.TextField()
class AuthorBook(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
book = models.ForeignKey(Book, on_delete=models.CASCADE)In the example above, we've created three models:
Author: A model for authors.Book: A model for books, with a ForeignKey relationship to Author.BookReview: A model for book reviews, with a ForeignKey relationship to Book.AuthorBook: A model representing the many-to-many relationship between Author and Book.on_delete to define the behavior when a related object is deleted.blank=True and null=True for ForeignKey fields if they can be empty.from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE, blank=True, null=True)
title = models.CharField(max_length=100)That's it for today! In the next lesson, we'll learn more about Django's Model methods and how to create and manage instances. Keep coding, and happy learning! π
π Note: Be sure to run migrations after defining your models:
python manage.py makemigrations
python manage.py migrate