Worthless-Stuff the Model
To continue with my building of worthless-stuff.com using Django, we’ve come to the point where we need to start creating the piece that everything else is built around. Django says this about models:
A model is the single, definitive source of data about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.
So for my site, I need to capture the following information:
- Title
- Slug field that is derived from the title (built into Django)
- Date
- Author
- Text area for body text
- An optional image(which will also need to be uploaded)
- A ranking of some sort
- Multiple Tags (for easy searching and Tagging to other Web services)
- An option as to whether or not comments are enabled
- A status. Is this a draft or published
So lets start with the easy stuff and save the ranking for later. There is a possibility I’m going to use the django-voting application, but I’m not too familiar with it yet.
Since I have decided to just get the blog stuff up and running, I’m going to create a simple model that will allow me to enter a post and start to build out the website. I would say that it’s usually a better idea to plan out your design, but in this case I just want to get something up quickly, and I’m sure my original thoughts and desired design will change over the course of this project. The following is what I have created for my model of the blog.
from django.db import models
from django.contrib.auth.models import User
#Publish Options
PUBLISH_OPTIONS = (
('DRAFT', 'Draft'),
('PUBLISHED','Published'),
)
class Tag(models.Model):
tag_name = models.CharField(maxlength=50)
def __str__(self):
return self.tag_name
class Admin:
pass
class Post(models.Model):
title = models.CharField(maxlength=80)
body = models.TextField()
slug = models.SlugField(prepopulate_from=("title",))
tag = models.ManyToManyField(Tag)
pub_date = models.DateTimeField()
author = models.ForeignKey(User)
comments_enabled = models.BooleanField(default=True)
status = models.CharField(maxlength=9, choices=PUBLISH_OPTIONS)
image = models.ImageField(upload_to='/worthless-stuff/media/images', blank=True, null=True, help_text="Image shouldn't be more than 200px wide")
class Admin:
fields = (
('Main Entry', {'fields': ('title', 'body', 'tag', 'image')}),
('Date Info', {'fields': ('pub_date',)}),
('Metadata', {'fields': ('author', 'comments_enabled','slug','status',)}),
)
list_display = ('title','pub_date', 'author', 'status')
list_filter = ['pub_date','status','comments_enabled']
search_fields = ['title', 'slug']
Without getting into to much detail, I have two classes that describe my data. There is the Tag class and the Post class. The Tag class is simply one-to-many class that allows me to have as many tags as I want per post. Everyone is tagging these days, so this is my version of it implemented in my blog. The Post class, is really the essential parts of the blog that need to be created. This is really the stuff people will read. The title, the date is was published, which will come in really handy when we get to our views, and other important pieces such as author, an optional image and whether or not comments will be enabled. Below that I added some Django freebies that allow me to display my fields any way I like them and then a search option for the admin. You really ought to spend a lot of time on the admin, since that is where you will spend most of your time once the site is up and running. There are so many wonderful features that come out of the box it’s amazing.
So once I put that information in my models.py file in the blog directory, I ran the following command from the myproject directory.
python manage.py syncdb
This then validates the model and will tell you if you have any issues. The debugging information is pretty good, and I had a comma out of place, which it found and told me. Once that’s all done issue:
pkill python
Now log into your admin http://yourdomian/admin and marvel at all the hard work the guys at djangoproject.com have given you for free. Now we have to move on to creating our templates so we can show off the content we are going to put into our blog.

























