Implementing a Database

Django is pretty dope. One reason why it's dope is because we don't really have to understand databasing too well. What we do need to understand, though, is Object Oriented Programming in Python!

In django, we represent each table as a python class and each column is a class attribute.

The only thing we need to think about are these things called Model Fields.

Model Fields

We're making a SQL database. Like most programming languages, SQL has its types and its own way of representing data and all that fancy stuff. To comply, we store our class attributes as Model Fields. Read more about them at this link (also, I always use this for reference so it's a good one to keep handy) : https://docs.djangoproject.com/en/1.10/ref/models/fields/

Code

This is finally the code that'll go in database/models.py:

from django.db import models

# Create your models here.

class Post(models.Model):
    imgURL = models.CharField(max_length=400)
    caption = models.TextField(default="")
    postedBy = models.CharField(max_length=50)

class Like(models.Model):
    likedBy = models.CharField(max_length=50)
    post = models.ForeignKey(Post, related_name="likes")

results matching ""

    No results matching ""