---
% Django Models - Blog Posts Module
% Will Holderness
---
Create BlogPost Model by adding a new class into mainapp/models.py. Name the class BlogPost and make it extend models.Model. In the class body, add field called title and set it to models.CharField(). Pass max_length of 200 to the CharField method.
class BlogPost(models.Model):
title = models.CharField(max_length=200)To add the foreign key, we need to know which user model is in use. This is stored in the Django setting called AUTH_USER_MODEL. Lets first import those settings.
Add from django.conf import settings to the top of the file. Then in the body of our BlogPost class, add a new field called author with type of models.ForeignKey(). Pass the following parameters to ForeignKey(): model=settings.AUTH_USER_MODEL, related_name="posts", and on_delete=models.CASCADE.
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
related_name="posts",
on_delete=models.CASCADE,
)Add another field to BlogPost called body with type models.TextField()
Add a field called postdate of type models.DateTimeField() with parameters auto_now_add set to True, and blank set to True.
postdate = models.DateTimeField(auto_now_add=True, blank=True)
Adding a str() method to the class will simplify rendering in admin view and our templates. In the BlogPost class, create a method called str that takes a parameter called self. Have the method return self.title.
def str(self):
return self.titleHaving a get_absolute_url function lets Django determine the canonical URL for a given model. This will come in handy in our views. First we need to import the reverse helper function from django.urls at the top of the file. from django.urls import reverse. Next add a method in the class called get_absolute_url that takes the self parameter. The method should contain the following line: return reverse('post', args=[str(self.id)])
def get_absolute_url(self):
return reverse('post', args=[str(self.id)])In mainapp/admin.py, import the new BlogPost model from .models. Then at the bottom of the file, call admin.site.register() with BlogPost as the only parameter.
from .models import BlogPost
admin.site.register(BlogPost)Now we need to update mainapp/views.py to include our new BlogPost model. First lets import the model with import BlogPost model from .models. Also import the get_object_or_404 method from django.shortcuts with import get_object_or_404 method from django.shortcuts.
Next in the index method, set the posts variable to BlogPosts.objects.all() instead of ALL_POSTS.
In the post method, we can take advantage of the django shortcut get_object_or_404() to do all the exception handling instead of having to rewrite it often. Replace the entire try/accept block with a line that sets the post variable to the BlogPost returned by get_object_or_404(BlogPost, pk=id) .
The return line can stay just as it is.
Now that we’ve added our BlogPost model, we need to run migrations again. From the command line, inside the root of the project, run the following commands:
python manage.py makemigrations
python manage.my migrate
Note: Make sure your venv is activated with source venv/bin/activate