8. Django App & Model

For this Django project, we’re going to be performing scheduled web scraping events. This chaper is all about creating our first Django app without any additional integrations.

8.1. Stocks app

In Create Django App, we started our stocks app with the following:

python manage.py startapp stocks

Do this now if you haven’t already.

8.2. stocks/models.py

Eventually, we’ll have celery and django work in concert by web scraping stock market prices for major public companies in the United States.

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


STOCK_MARKET_LOOKUP_SOURCES = (
    ('business_insider', "Business Insider Markets"),
    ('google_finance', "Google Finance"),
    ('echo', "HTTPbin Echo"),
)

class Company(models.Model):
    name = models.CharField(max_length=120)
    ticker = models.CharField(max_length=20)
    description = models.TextField(blank=True,null=True)
    active = models.BooleanField(default=True)
    timestamp = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)


class PriceEvent(models.Model):
    company = models.ForeignKey(Company, null=True, on_delete=models.SET_NULL)
    name = models.CharField(max_length=120, verbose_name='Company Name')
    price = models.DecimalField(max_digits=10, decimal_places=2)
    source = models.CharField(max_length=50, choices=STOCK_MARKET_LOOKUP_SOURCES, default='echo')
    timestamp = models.DateTimeField(auto_now_add=True)