Skip to content

Commit aa245a3

Browse files
committed
Combine the 'article' & its related comments together (backend only till now)
1 parent 976ad32 commit aa245a3

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

chapter08/README.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,35 @@
420420
### Goals
421421
- Add a dedicated ```comments``` app and link it to ```articles```
422422

423-
### Steps
423+
### Get an model! (i.e. **comment**)
424424
- basis
425425
- what to add
426426
- FILE: articles/**models.py**
427427
- CLASS: ```Comment(models.Model)``` (newly added)
428428
- and
429429
- ```./manage.py makemigrations articles```
430430
- ```./manage.py migrate```
431-
431+
- lastly
432+
- articles/**admin.py**
433+
1. ```from .models import Article, Comment```
434+
2. ```admin.site.register(Comment)```
435+
- only by doing this then the app will visible (displaying on website (?front/back))
436+
437+
### Now we have it at the **backend**! But it could be better
438+
- What to solve (quote from author)
439+
> ... "wouldn't it be better to just see all ```Comment``` models related to a single ```Post``` model?" ...
440+
- What to solve
441+
1. The ```articles``` & ```comments``` app both needed to open seperately, which is **NOT** makes sense (right? 😅)
442+
2. So, let's put it in the jar! (Nope) ***put the ```articles``` & ```comments``` on the same screen***!
443+
- How to solve
444+
- Fact
445+
- Django provides two main *inline views*: ```TabularInline```, ```StackedInline``` (tiny differences btw)
446+
- Code
447+
- modify the **admin.py**
448+
- What will be changed
449+
- Only the **admin.py**
450+
- Here we go (solutions)!
451+
- well, not clearly sayin, it's *wrapping* & *superclass* stuff :P
452+
- summary (no detailed code, I didn't fully understand it yet)
453+
1. two new classes added
454+
2. one more to register (to admin)

chapter08/articles/admin.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
from django.contrib import admin
22

33

4-
from .models import Article
4+
from .models import Article, Comment
55

66

7-
admin.site.register(Article)
7+
class CommentInline(admin.TabularInline):
8+
""" The only difference here
9+
is the superclass being passed in :P
10+
11+
Either `StackedInline` or `TabularInline`,
12+
13+
Well, I prefer the latter! Cleaner & nicer :)
14+
"""
15+
16+
model = Comment
17+
18+
19+
class ArticleAdmin(admin.ModelAdmin):
20+
inlines = [
21+
CommentInline,
22+
]
23+
24+
25+
# well.. the orders here do matter to the execution!
26+
admin.site.register(Article, ArticleAdmin)
27+
admin.site.register(Comment)

0 commit comments

Comments
 (0)