Django Models Deep Dive
Complete the Blog Post ModelEasyWrite Code
2/5
Incomplete Code

Description

A junior developer started writing a `Post` model for a blog app but left several fields incomplete. Fill in the missing field definitions to match the requirements. The model should represent a blog post with a title, body, author reference, and automatic timestamps.

Requirements

01Use `CharField` for title with max_length=200
02Use `TextField` for body
03Add a `ForeignKey` to `settings.AUTH_USER_MODEL` with `on_delete=CASCADE` and `related_name='posts'`
04Add `created_at` with `auto_now_add=True`
05Add `updated_at` with `auto_now=True`
06Implement `__str__` returning the title
07Set `ordering = ['-created_at']` in Meta

Example

>>> post = Post(title="Hello World", body="...", author=user)
>>> str(post)
'Hello World'
>>> Post._meta.ordering
['-created_at']
Python 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Ln 16, Col 1