Every Django model is a Python class inheriting from models.Model. Class-based views (CBVs) encapsulate HTTP handling in classes. OOP is central—not optional.
Model classes
class Article(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.title
__str__ improves admin and shell readability. Add methods for domain logic: def is_published(self): ...
Inheritance
- Abstract base models share fields across apps
- CBVs inherit from
View,ListView,CreateView - Proxy models change behavior without new tables
Important interview questions and answers
- Q: FBV vs CBV?
A: Function-based views are explicit; class-based views reuse generic patterns (list, detail, form)—pick clarity over cleverness. - Q: What is
Metaon a model?
A: Inner class for options:ordering,verbose_name_plural,db_table. - Q:
__str__vs__repr__?
A:__str__for humans (admin);__repr__for developers debugging in shell.
Self-check
- What do all Django models inherit from?
- Why define
__str__on a model?
Interview prep
- FBV vs CBV?
Function views are explicit; class-based views reuse generic patterns (ListView, CreateView)—pick clarity over abstraction for complex flows.