Django Fundamentals
Define a Blog Post ModelEasyWrite Code
2/10
models.Model + fields

Description

Create a Django model called `Post` in your `blog` app that represents a blog post with the following fields: title, body, author, and timestamps for creation and last update. The model should use appropriate field types and include a `__str__` method that returns the post title.

Requirements

01Define a `Post` model inheriting from `models.Model`
02Use `CharField` for title (max 200 chars)
03Use `TextField` for body content
04Use `ForeignKey` to link to Django's built-in `User` model
05Add `created_at` with `auto_now_add=True` and `updated_at` with `auto_now=True`
06Implement `__str__` to return the title
07Add a `Meta` class with `ordering = ['-created_at']`

Example

>>> post = Post(title="Hello World")
>>> str(post)
'Hello World'
>>> Post.objects.all().query  # should order by -created_at
Python 3
1
2
3
4
5
6
7
8
9
10
11
Ln 11, Col 1