Guided Notes

Models & ORM

Defining data shape and querying it.

Defining models

Slide 1 / 5

# library/models.py from django.db import models

class Author(models.Model): name = models.CharField(max_length=100)

class Book(models.Model): title = models.CharField(max_length=200) author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name="books") year = models.IntegerField() genre = models.CharField(max_length=50) available = models.BooleanField(default=True)

Each class is a table; each field is a column. Django adds an id primary key automatically.

← Templates