Back to articles
Day 67 of 100 Days Of Code — Django Models and ORM
How-ToSystems

Day 67 of 100 Days Of Code — Django Models and ORM

via Dev.toM Saad Ahmad

Yesterday, I learned URLs, views, and templates. Everything was hardcoded, though. Today, for day 67, I connected Django to an actual database. Models, migrations, the ORM, and the admin panel. By the end, I had a page pulling real data from a real database and displaying it in the browser. What is a Model? A model is a Python class that represents a database table. Each attribute is a column. Django handles all the SQL; you never write CREATE TABLE or SELECT * directly. from django.db import models class Post ( models . Model ): title = models . CharField ( max_length = 200 ) content = models . TextField () created_at = models . DateTimeField ( auto_now_add = True ) is_published = models . BooleanField ( default = False ) def __str__ ( self ): return self . title This class tells Django to create a post table with four columns. The __str__ method controls how a Post object is displayed in the admin panel, in the shell, everywhere. Common Field Types Field Use CharField Short text, req

Continue reading on Dev.to

Opens in a new tab

Read Full Article
2 views

Related Articles