The model is your database structure. Lets start by opening the models.py file, and start adding some fields. Because I am keeping this simple, I will not be including users at this stage.

class Blog(models.Model):
    title = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    body = models.TextField()
    posted = models.DateField(db_index=True, auto_now_add=True)
    category = models.ForeignKey('blog.Category')

class Category(models.Model):
    title = models.CharField(max_length=100, db_index=True)
    slug = models.SlugField(max_length=100, db_index=True)

Now lets see what each part means.

This creates a database table with the name "Blog". This need to be something obvious and will be used a lot.

class Blog(models.Model):

These are basic fields to be created in your database

title = models.CharField(max_length=100, db_index=True)
slug = models.SlugField(max_length=100, db_index=True)
body = models.TextField()
posted = models.DateTimeField(db_index=True, auto_now_add=True)

The last field, a little more advanced. This field populates its data from another database table, in this case Category, so you will need to populate the Category table field first.

category = models.ForeignKey('blog.Category')

Finish the model

There are a few more things to be added to the model now that we have decided on the database structure. Your completed model file should look as follows.

from django.db import models
from django.db.models import permalink

# Create your models here.

class Blog(models.Model):
    title = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    body = models.TextField()
    posted = models.DateTimeField(db_index=True, auto_now_add=True)
    category = models.ForeignKey('blog.Category')

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('view_blog_post', None, { 'slug': self.slug })

class Category(models.Model):
    title = models.CharField(max_length=100, db_index=True)
    slug = models.SlugField(max_length=100, db_index=True)

    def __unicode__(self):
        return '%s' % self.title

    @permalink
    def get_absolute_url(self):
        return ('view_blog_category', None, { 'slug': self.slug })

What are all these extras for?

The __unicode__ function sets the text reference for each record. This is used mainly in the automated django admin, but this is still available to use on your own site.

The get_absolute_url function defines a URL, again used in the admin area, for each record.

Without the @permalink decorator the following would not work. This returns a URL calculated from the urls.py file which will be explained shortly. I would recommend using this method as it allows you to change the URL for a page in only one location.

return ('view_blog_post', None, { 'slug': self.slug })

Example

Title: How to create a basic blog in django
Response from __unicode__: How to create a basic blog in django
Response from get_absolute_url: /blog/view/how-to-create-a-basic-blog-in-django.html

Limitations from this example

  • Blog has a field slug. This is used as the URL to identify the post. In this case both the title & slug field are set to unique. It is not unreasonable to have two posts with the same Title. To solve this you could set the slug field to contain the ID of the post ie. 2-my-second-post
  • This example allows only one category per post. This is quite simple to change by adding a ManyToMany field type to category instead of ForeignKey. The way you use this is sligthly different, so it will not work without other changes to this tutorial.