Day 1 of 5
⏱ ~60 minutes
Django in 5 Days — Day 1

Models and the Django ORM

Create a Django project, define your first models, run migrations, and query the database — no raw SQL required.

Starting a Django Project

Django's tooling creates a ready-to-run project structure in seconds.

Terminal
pip install django
django-admin startproject mysite .
python manage.py startapp blog
# Add 'blog' to INSTALLED_APPS in settings.py
python manage.py runserver
blog/models.py
from django.db import models
from django.contrib.auth.models import User

class Post(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)
    body = models.TextField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    published = models.BooleanField(default=False)

    class Meta:
        ordering = ['-created_at']

    def __str__(self):
        return self.title
Terminal
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
Django Shell — QuerySet API
python manage.py shell
>>> from blog.models import Post
>>> Post.objects.all()
>>> Post.objects.filter(published=True)
>>> Post.objects.filter(title__icontains='django')
>>> Post.objects.exclude(published=False)
>>> Post.objects.order_by('-created_at')[:5]
>>> post = Post.objects.get(slug='my-first-post')
>>> post.title
>>> Post.objects.count()
💡
Register your model in blog/admin.py: from .models import Post; admin.site.register(Post). Then visit /admin with your superuser credentials. You get a full CRUD UI for free.
📝 Day 1 Exercise
Build a Blog Database
  1. C
  2. r
  3. e
  4. a
  5. t
  6. e
  7. t
  8. h
  9. e
  10. P
  11. o
  12. s
  13. t
  14. a
  15. n
  16. d
  17. C
  18. a
  19. t
  20. e
  21. g
  22. o
  23. r
  24. y
  25. m
  26. o
  27. d
  28. e
  29. l
  30. s
  31. .
  32. R
  33. u
  34. n
  35. m
  36. i
  37. g
  38. r
  39. a
  40. t
  41. i
  42. o
  43. n
  44. s
  45. .
  46. A
  47. d
  48. d
  49. s
  50. a
  51. m
  52. p
  53. l
  54. e
  55. d
  56. a
  57. t
  58. a
  59. i
  60. n
  61. t
  62. h
  63. e
  64. a
  65. d
  66. m
  67. i
  68. n
  69. .
  70. Q
  71. u
  72. e
  73. r
  74. y
  75. a
  76. l
  77. l
  78. p
  79. o
  80. s
  81. t
  82. s
  83. b
  84. y
  85. a
  86. s
  87. p
  88. e
  89. c
  90. i
  91. f
  92. i
  93. c
  94. a
  95. u
  96. t
  97. h
  98. o
  99. r
  100. a
  101. n
  102. d
  103. a
  104. l
  105. l
  106. p
  107. u
  108. b
  109. l
  110. i
  111. s
  112. h
  113. e
  114. d
  115. p
  116. o
  117. s
  118. t
  119. s
  120. u
  121. s
  122. i
  123. n
  124. g
  125. t
  126. h
  127. e
  128. s
  129. h
  130. e
  131. l
  132. l
  133. .

Day 1 Summary

  • django-admin startproject and startapp scaffold your project structure.
  • Models are Python classes. Fields map to database columns. Django creates the SQL.
  • makemigrations generates migration files. migrate applies them to the database.
  • The QuerySet API: .filter(), .exclude(), .get(), .order_by(), .count().
Finished this lesson?