Section 2 : Creating Django Model for database interaction
Django models are a crucial component of Django's Object-Relational Mapping (ORM) system, allowing developers to define the structure of their database tables as Python classes. Each model class represents a table in the database, and its attributes define the columns. Django automatically handles creating, updating, and querying the database through these model classes, eliminating the need for manual SQL. This ORM simplifies interactions with the database and improves code maintainability, making Django a powerful framework for web development.
Here is the code for the StockPrice model:
from django.db import models
class StockPrice(models.Model):
status = models.CharField(max_length=10)
date = models.DateField()
symbol = models.CharField(max_length=10)
open_price = models.DecimalField(max_digits=10, decimal_places=2)
high = models.DecimalField(max_digits=10, decimal_places=2)
low = models.DecimalField(max_digits=10, decimal_places=2)
close_price = models.DecimalField(max_digits=10, decimal_places=2)
volume = models.BigIntegerField()
after_hours = models.DecimalField(max_digits=10, decimal_places=2)
pre_market = models.DecimalField(max_digits=10, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'stock_prices' # Explicitly define the table name in the database
def __str__(self):
return f"{self.symbol} - {self.date}"
What are Migrations and How to Run them
Django Migrations are a way to manage changes to your database schema over time. They allow you to create, modify, and delete database tables and fields automatically, based on changes in your Django models. Migrations track these changes using versioned files, enabling easy database updates and version control.
1. Creating Migrations
python3 manage.py makemigrations stock_price_api
2. Running the migrations
python3 manage.py migrate
Load Sample Data
Let’s add some test data in the database:
INSERT INTO stock_price (status, date, symbol, open_price, high, low, close_price, volume, after_hours, pre_market, created_at, updated_at)
VALUES ('active', '2024-11-01', 'AAPL', 180.50, 182.75, 179.80, 181.00, 12000000, 181.25, 180.00, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO stock_price (status, date, symbol, open_price, high, low, close_price, volume, after_hours, pre_market, created_at, updated_at)
VALUES ('inactive', '2024-11-02', 'GOOGL', 2650.30, 2680.40, 2620.00, 2675.50, 8000000, 2660.00, 2640.00, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO stock_price (status, date, symbol, open_price, high, low, close_price, volume, after_hours, pre_market, created_at, updated_at)
VALUES ('active', '2024-11-03', 'MSFT', 330.75, 335.50, 329.20, 333.30, 9000000, 332.80, 331.50, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO stock_price (status, date, symbol, open_price, high, low, close_price, volume, after_hours, pre_market, created_at, updated_at)
VALUES ('active', '2024-11-04', 'TSLA', 800.20, 820.00, 790.50, 810.00, 15000000, 815.50, 805.00, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
INSERT INTO stock_price (status, date, symbol, open_price, high, low, close_price, volume, after_hours, pre_market, created_at, updated_at)
VALUES ('active', '2024-11-05', 'AMZN', 3500.75, 3550.00, 3480.50, 3525.00, 7000000, 3530.00, 3495.00, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);