Models and Admin

View saved

Goal for this part

Move Campus Press from temporary Python lists to lasting data. You will define a Post model, create migrations, apply them, register the model with the admin, and display real database rows on home.

After this snapshot, restarting the server should keep your posts, and you can create or edit them at /admin/.

What you should already know

Comfort with templates and the idea of structured data is enough. Some SQL vocabulary helps—tables, rows, primary keys—but Django’s ORM lets you work mostly in Python classes.

If SQL is brand new, skim the linked “What is SQL?” and SQLite lessons. You do not need to write raw SQL for this part.

Concepts: models, migrations, and the admin

A model class describes a database table. Fields become columns; ForeignKey connects each post to a user. makemigrations records schema changes; migrate applies them.

Django’s admin is a built-in staff UI. Register Post with admin.site.register(Post), create a superuser, and you can manage content without writing forms yet.

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

Walkthrough: migrate, superuser, and query

After saving the model, run makemigrations then migrate. Create a staff account with createsuperuser, sign in at /admin/, and add a few posts.

Update the home view to query Post.objects.all() (or order by newest) and pass posts into the template—same shape as the old list, but backed by SQLite.

from django.shortcuts import render
from .models import Post

def home(request):
    posts = Post.objects.all().order_by("-date_posted")
    return render(request, "blog/home.html", {"posts": posts})

How to run it and explore

Start the 04-Models-And-Admin snapshot, migrate if needed, create a superuser when prompted by the lesson notes in the repo, and open admin plus home. Stop and start again: posts should still be there.

git clone https://github.com/michaeldunga1/fcc-django-blog.git
cd fcc-django-blog/04-Models-And-Admin
python3 -m venv .venv
source .venv/bin/activate
pip install -r ../requirements.txt
python manage.py migrate
python manage.py runserver

# in another terminal (venv on):
python manage.py createsuperuser
python manage.py shell
>>> from blog.models import Post
>>> Post.objects.all()

Common mistakes and troubleshooting

Errors about no such table usually mean you forgot migrate. Admin 404 can mean admin/ is missing from root URLs. If posts do not appear on home, confirm the view queries the model and the template loops over the same context key.

Changing a field after migrate requires a new migration—do not edit the database by hand while learning.

Try this

Add a post in admin, refresh home, then change the title in admin and confirm the site updates. In the shell, filter posts by author.

  • Models describe tables; instances behave like rows
  • Migrations keep schema changes reproducible
  • Admin is a staff tool, not the public create-post UI yet

Next: User Registration

Comments

One comment per signed-in account. Comments are saved with this page’s URL.