Composer and Programmer
A Django Blog: 2. The Post Model
And welcome back to my tutorial on how to create a functioning blog using django! I apologize for the delay, but I’ve been swamped with mid-terms and work. If you have yet to read part 1 of the tutorial, please read it now.
The Post Model
Models are simply python representations of objects stored in the database. Each comment, post, user, and other items that are stored in the database are represented by models. For this tutorial, we only need the Post model. The Comment model will be dealt with later.
Start by opening the blog/models.py file up in your favorite editor. You will be presented with a rather simple file that contains one line of code and a comment. Replace the contents of the file with the following:
from django.db import models from django.contrib.auth.models import User class Post(models.Model): title = models.CharField(max_length=100) content = models.TextField() user = models.ForeignKey(User, null=True, blank=True) is_published = models.BooleanField(default=True) created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) def __unicode__(self): return self.title class Meta: ordering = ['-created']
Let’s take a look at this code a section at the time.
Inclusions
from django.db import models from django.contrib.auth.models import User
The first line imports the models module. It gives us the base class which we will build our models off of and all of the fields we will add to the model. The second line imports a model called User.
Declaring the model
class Post(models.Model):
A model is simply a class (object). This line defines the class, names it (Post), and tells it to inherit all the properties of the Model class (models.Model).
Declaring the properties
title = models.CharField(max_length=100)
This first property creates a title for our post. This becomes a character field of at most 100 characters.
content = models.TextField()
The content becomes the actual post’s content. As we want to handle a larger amount of text, we use TextField instead. Remember, CharField for small amounts of text, TextField for longer.
user = models.ForeignKey(User, null=True, blank=True)
Let’s say we have multiple authors. Well, we want to map each post to a user. Thus we include a ForeignKey that points to the User model. Behind the scenes, it will create a small integer field that will contain the id of the user selected. However, if a user is not set, then we allow the field to be null (can be unset) and blank (allows the admin site to set it to null) so the post can have an anonymous author.
is_published = models.BooleanField(default=True)
This gives a flag that signifies if the post was published (publicly visible). By default, we set this to true as we assume that the poster wants the post to be published immediately.
created = models.DateTimeField(auto_now_add=True)
We want to keep track of when the post was created so we have something to sort by. Yes, you can set the date and time manually, but it’s easier to have django do it for us when we add it. auto_now_add will set the time appropriately upon the first save.
updated = models.DateTimeField(auto_now=True)
Finally, we want to keep track of when the post was last updated. To make it easier, this is also automatically set every time the post is saved.
A Human-Readable Name
def __unicode__(self): return self.title
The default name given to each model is not that great in appearance. So, to make it look nicer, we define a function that returns the title of the post.
Setting the order
class Meta: ordering = ['-created']
As we want the posts sorted by the date and time at which they were posted, we want to sort descending by the created timestamp. This way, the latest posts appear first and the oldest appear last. We define this in a subclass called Meta, which just defines extra parameters. For this example, we only need to worry about order. There are other parameters such as verbose_name_plural, but you don’t need that unless you want to stop django from spelling the plural of music as musics or spelling activities as activitys.
Creating the Database
Finally, it’s time to actually create our database. Go back into the terminal, browse to the myblog project directory if you aren’t there already, and type the following command:
python manage.py syncdb
There will be a bunch of lines that will appear on the screen about creating tables. You will be asked to create a superuser, so type in yes. Fill in a username, password, and email address as these will be used for the first user account. When done, we are ready to start working on getting the admin site set up! However, you have to wait for part three for that.
