Checkpoint: CRUD and architecture¶
You have gone through the full cycle from domain design to a finished application with PostgreSQL and CBVs. Time to make sure everything is clear.
What you can do after this step¶
Design¶
- [ ] I can describe a domain in words before writing code
- [ ] I know the 7 steps of the methodology: Domain → Entities → Attributes → Relationships → Normalization → on_delete → Django Model
- [ ] I understand the difference between
OneToOneField,ForeignKey,ManyToManyField - [ ] I know where the FK lives (on the "Many" side)
- [ ] I deliberately choose
on_deletefor every FK (not just CASCADE) - [ ] I can draw an ER diagram in Mermaid
Models and migrations¶
- [ ] I can write models with
related_name,__str__,Meta.ordering - [ ] I understand
null=Truevsblank=True(DB vs form) - [ ] I know the migration lifecycle:
makemigrations→sqlmigrate→migrate - [ ] I never edit a migration that has already been applied
- [ ] I always give migrations descriptive names (
--name)
Architecture¶
- [ ]
selectors.pycontains only SELECTs (filter, annotate, prefetch_related) - [ ]
services.pyusestransaction.atomic()where atomicity is needed - [ ]
views.pydoes not containModel.objects.*directly - [ ] I understand the READ flow and the WRITE flow
ORM¶
- [ ] I understand N+1 and use
select_related/prefetch_related - [ ]
Qobjects — for OR conditions in filter - [ ]
F()— for atomic operations and comparing fields - [ ]
select_for_update()— for preventing race conditions - [ ]
transaction.atomic()— for atomic groups of operations - [ ]
annotate()— for computing in SQL instead of Python
CBV¶
- [ ] I understand the difference between FBV and CBV
- [ ] I know why
.as_view()is needed inurls.py - [ ] I know the MRO order:
LoginRequiredMixinFIRST - [ ]
UserQuerySetMixin— DRY data isolation per user - [ ]
form_valid()instead ofsuper().form_valid()(so as not to bypass services) - [ ]
reverse_lazyinstead ofreversein class attributes
PostgreSQL¶
- [ ] I can run PostgreSQL via Docker
- [ ] I can configure
DATABASE_URLviapython-decouple - [ ] I can move data from SQLite via
dumpdata/loaddata
Common mistakes¶
Mistake 1: Starting with models.py¶
❌ WRONG: we immediately write class Note(models.Model)
✅ CORRECT: first describe the domain in words, then the ER diagram, then the code
Mistake 2: A QuerySet in the View¶
# ❌ WRONG:
def note_list(request):
notes = Note.objects.filter(user=request.user).select_related(...)
return render(...)
# ✅ CORRECT:
def note_list(request):
notes = selectors.get_user_notes(request.user)
return render(...)
Mistake 3: Direct mutation in the View¶
# ❌ WRONG:
def note_create(request):
if form.is_valid():
note = form.save() # bypasses services!
# ✅ CORRECT:
def note_create(request):
if form.is_valid():
note = services.create_note(user=request.user, ...)
Mistake 4: N+1 when accessing an FK¶
# ❌ WRONG:
notes = Note.objects.all()
for note in notes:
print(note.notebook.title) # N+1!
# ✅ CORRECT:
notes = Note.objects.select_related('notebook').all()
for note in notes:
print(note.notebook.title) # 1 query!
Mistake 5: SET_NULL without null=True¶
# ❌ WRONG:
notebook = models.ForeignKey(Notebook, on_delete=models.SET_NULL)
# Django system check: fields.E320 — SET_NULL requires null=True on the field!
# ✅ CORRECT:
notebook = models.ForeignKey(
Notebook, on_delete=models.SET_NULL,
null=True, blank=True,
)
Mistake 6: super().form_valid() in a CBV¶
# ❌ WRONG in a CBV with services:
def form_valid(self, form):
super().form_valid(form) # form.save() is in there — bypasses services!
# ✅ CORRECT:
def form_valid(self, form):
note = services.create_note(user=self.request.user, ...)
return redirect('hello_app:note_detail', pk=note.pk)
Mistake 7: reverse() instead of reverse_lazy() in a class attribute¶
# ❌ WRONG: executed when the class is loaded
class NoteDeleteView(DeleteView):
success_url = reverse('hello_app:note_list') # NoReverseMatch!
# ✅ CORRECT: executed lazily during the request
class NoteDeleteView(DeleteView):
success_url = reverse_lazy('hello_app:note_list')
Practical exercise¶
- Draw an ER diagram for a "Library" system:
Book,Author,Genre,Review. Specify the relationship types andon_deletefor every FK. - Implement the models with correct
related_name,__str__,ordering. - Write
get_books_by_genre(genre)inselectors.pywithselect_related('author').prefetch_related('genres'). - Write
create_book(*, title, author_id, genre_ids)inservices.pywithtransaction.atomic(). - Check in the Django shell: change the
authorof a book and verify thatupdated_atwas updated. - Rewrite
BookListViewandBookCreateViewfrom FBV to CBV withLoginRequiredMixinandUserQuerySetMixin. - Make sure there is no N+1: install
django-debug-toolbarand check the number of queries on the book list page.
Navigation¶
- Previous: Class-Based Views
- Back to the index: Step 3. CRUD and architecture
- Next step: Step 4. Templates and Forms