Learning to Code ORM: Shrubbery Part Five
Shrubbery Part 5 - ORM
Overview
In this part of the assignment, you will refactor the Simple Shrubbery Storefront from Part 4 to use Django and its built-in ORM for database management. You'll learn how to create a Django application, define models, and perform CRUD operations using the ORM.
Objectives
By the end of this assignment, you will be able to:
- Set up a Django web application
- Use Django's built-in ORM to manage data
- Perform CRUD operations for Shrubbery and Transaction models using Django ORM
Instructions
- Install Django by running
pip install django
. - Create a new Django project by running
django-admin startproject shrubbery_store
. This will create a new folder namedshrubbery_store
with the basic Django project structure. - Change the working directory to the newly created project folder by running
cd shrubbery_store
. - Create a new Django app within the project by running
python manage.py startapp store
. This will create a new folder namedstore
with the basic Django app structure. - Define the
Shrubbery
,Transaction
, andTransactionItem
models in themodels.py
file of thestore
app:
from django.db import models
class Shrubbery(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=10, decimal_places=2)
def __str__(self):
return self.name
class Transaction(models.Model):
transaction_date = models.DateTimeField()
total_amount = models.DecimalField(max_digits=10, decimal_places=2)
def __str__(self):
return f"Transaction {self.id} - {self.transaction_date}"
class TransactionItem(models.Model):
transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE)
shrubbery = models.ForeignKey(Shrubbery, on_delete=models.CASCADE)
quantity = models.IntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2)
def __str__(self):
return f"TransactionItem {self.id} - {self.shrubbery.name}"
- Run
python manage.py makemigrations store
andpython manage.py migrate
to create the tables in the SQLite database. - Refactor the
Store
class from Part 4 to use the Django ORM for CRUD operations. You can remove the__init__
andsetup_database
methods, as the Django ORM will handle the database connection:
from store.models import Shrubbery, Transaction, TransactionItem
class Store:
def display_shrubberies(self):
shrubberies = Shrubbery.objects.all()
for index, shrubbery in enumerate(shrubberies, start=1):
print(f"{index}. {shrubbery.name} - ${shrubbery.price}")
def add_shrubbery(self, shrubbery):
new_shrubbery = Shrubbery(name=shrubbery.name, price=shrubbery.price)
new_shrubbery.save()
def update_shrubbery(self, shrubbery):
updated_shrubbery = Shrubbery.objects.get(id=shrubbery.id)
updated_shrubbery.name = shrubbery.name
updated_shrubbery.price = shrubbery.price
updated_shrubbery.save()
def delete_shrubbery(self, shrubbery_id):
shrubbery = Shrubbery.objects.get(id=shrubbery_id)
shrubbery.delete()
def process_user_input(self, choice, cart):
# ... (existing code)
- Refactor the
Cart
class to use the Django ORM for checkout: