Django Models Deep Dive
Abstract Base Model with InheritanceHardWrite Code
5/5
Incomplete Code

Description

Build a content management system using Django's abstract base model pattern. You need a `TimeStampedModel` abstract base, and two concrete models (`Article` and `Video`) that inherit from it. This tests your understanding of abstract inheritance, custom managers, Meta inheritance, and model methods.

Requirements

01Create `TimeStampedModel` as an abstract model with `created_at` (auto_now_add) and `updated_at` (auto_now)
02Create a custom `PublishedManager` that filters `status='published'`
03Create `Article(TimeStampedModel)` with title, slug (unique), body, status (draft/published/archived using TextChoices), and author FK
04Create `Video(TimeStampedModel)` with title, url (URLField), duration_seconds (PositiveIntegerField), and status (reuse same TextChoices)
05Add `objects` (default manager) and `published` (PublishedManager) to both models
06Add `__str__` on both models returning the title
07Add `class Meta: ordering = ['-created_at']` on the abstract model so children inherit it
08Add a `is_published` property on TimeStampedModel (returns True if status == 'published')

Example

>>> Article.published.all()  # only published articles
>>> Article.objects.all()    # all articles
>>> a = Article(title="Test", status=Status.DRAFT)
>>> a.is_published
False
>>> Video._meta.ordering
['-created_at']  # inherited from abstract parent
Python 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Ln 51, Col 1