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:
This tutorial assumes you have a basic understanding of Python and Django. Prior experience with databases and SQL will be helpful, but not required.
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.
Now that we have a model, let's perform some CRUD (Create, Retrieve, Update, Delete) operations.
To create a new book:
new_book = Book(title="Django for Beginners", author="John Doe", publication_date="2020-01-01")
new_book.save()
To retrieve existing books:
books = Book.objects.all()
To update an existing book:
book = Book.objects.get(title="Django for Beginners")
book.publication_date = "2020-02-02"
book.save()
To delete a book:
book = Book.objects.get(title="Django for Beginners")
book.delete()
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()
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.
Publisher
with fields name
and website
.Publisher
instance and save it to the database.website
field of the Publisher
instance.Publisher
instances from the database and print their names and websites.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!