Day 2 - Installing Django and Database Models

Missed a couple of consecutive days, but work is keeping me busy. Still, let’s talk about my second day of learning…

Much of what I covered during this 1/2 hour of instruction was the installation of Django, using virtualenv, and setting up my first data model. I’m familiar with MVC frameworks; they are used all over the place. In the case of Django, it’s basically learning this design pattern all over again, though within the Pythonistic mindset.

Here’s a quick look at the code:

from django.db import models

# Create your models here.
class Article(models.Model):
    title = models.CharField(max_length=200)
    body  = models.TextField()
    pub_date = models.DateTimeField('date published')
    likes = models.IntegerField()

    def __unicode__(self):
        return self.title

Basically, this code is defining the database schema that will be used to create the Article table in the backend database. We’re saying that inside the database for this Django app, we want a table called ‘Article’ that has the following fields (or columns) defined:

Django converts this ‘model code’ into actual SQL statements, which are used to create the table. The syncdb command is the magic that performs this. After completing this portion of the lecture, we went into the python interactive shell, and actually created a couple of Articles (i.e row data in the table). Later today, I’ll tackle lecture 3.

Night all.

Jason T Clark

Jason T Clark

Father. Musician. Gamer. Coder.

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin