Using ORM with Django Models

Tutorial 4 of 5

1. Introduction

In this tutorial, we aim to explore Django's Object-Relational Mapper (ORM). This powerful feature allows developers to interact with their database, like you would with SQL. In other words, it's a way to create, retrieve, update, and delete records in your database.

By the end of this tutorial, you will:

  • Understand how to define models in Django
  • Learn how to perform common database operations using Django's ORM
  • Understand how to use Python to interact with your database

This tutorial assumes you have a basic understanding of Python and Django. Prior experience with databases and SQL will be helpful, but not required.

2. Step-by-Step Guide

Defining Models

In Django, a model is a Python class that represents a database table. Each attribute of the class represents a field of the table. Let's create a simple model:

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=100)
    publication_date = models.DateField()

In this example, Book is a Django model, which will be saved in a database table named Book. title, author, and publication_date are fields of the Book model.

CRUD Operations

Now that we have a model, let's perform some CRUD (Create, Retrieve, Update, Delete) operations.

Create

To create a new book:

new_book = Book(title="Django for Beginners", author="John Doe", publication_date="2020-01-01")
new_book.save()

Retrieve

To retrieve existing books:

books = Book.objects.all()

Update

To update an existing book:

book = Book.objects.get(title="Django for Beginners")
book.publication_date = "2020-02-02"
book.save()

Delete

To delete a book:

book = Book.objects.get(title="Django for Beginners")
book.delete()

3. Code Examples

Let's put it all together and look at a full example:

from django.db import models

# Define the model
class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=100)
    publication_date = models.DateField()

# Create a new book
new_book = Book(title="Django for Beginners", author="John Doe", publication_date="2020-01-01")
new_book.save()

# Retrieve all books
books = Book.objects.all()
for book in books:
    print(book.title)

# Update an existing book
book = Book.objects.get(title="Django for Beginners")
book.publication_date = "2020-02-02"
book.save()

# Delete a book
book = Book.objects.get(title="Django for Beginners")
book.delete()

4. Summary

In this tutorial, we learned how to define models in Django, perform CRUD operations, and interact with the database using Python. The next steps would be learning about more complex queries and understanding how to use relationships between models.

5. Practice Exercises

  1. Create a new Django model called Publisher with fields name and website.
  2. Create a new Publisher instance and save it to the database.
  3. Update the website field of the Publisher instance.
  4. Retrieve all Publisher instances from the database and print their names and websites.
  5. Delete the Publisher instance.

Solutions:

# 1. Define the Publisher model
class Publisher(models.Model):
    name = models.CharField(max_length=100)
    website = models.URLField()

# 2. Create a new Publisher instance
publisher = Publisher(name="New Publisher", website="https://www.newpublisher.com")
publisher.save()

# 3. Update the website field of the Publisher instance
publisher.website = "https://www.updatedpublisher.com"
publisher.save()

# 4. Retrieve all Publishers and print their names and websites
publishers = Publisher.objects.all()
for publisher in publishers:
    print(publisher.name, publisher.website)

# 5. Delete the Publisher instance
publisher.delete()

Keep practicing with different models and operations to deepen your understanding of Django ORM. Happy coding!