Open the terminal and run the following command to open the Python shell:
python manage.py shell
Then, type the following lines:
>>> from django.contrib.auth.models import User>>> from blog.models import Post>>> user = User.objects.get(username='admin')>>> post = Post(title='Another post', slug='another-post', body='Post body.', author=user)>>> post.save()
Let's analyze what this code does. First, we will retrieve the user object with the username admin:
user = User.objects.get(username='admin')
The get() method allows you to retrieve a single object from the database. Note that this method expects a result that matches the query. If no results are returned by the database, this method will raise a DoesNotExist exception, ...