{"id":158,"date":"2026-03-03T08:49:09","date_gmt":"2026-03-03T03:49:09","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=158"},"modified":"2026-03-17T08:45:17","modified_gmt":"2026-03-17T03:45:17","slug":"models-and-database","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/models-and-database\/","title":{"rendered":"Models and Database"},"content":{"rendered":"\n<p>In Django, <strong>Models<\/strong> define the structure of your database.<br>They allow you to create, read, update, and delete data using Python code instead of writing SQL directly.<\/p>\n\n\n\n<p>Django uses an ORM (Object Relational Mapper), which means you interact with the database using Python classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Model?<\/h2>\n\n\n\n<p>A Model is a Python class that represents a database table.<\/p>\n\n\n\n<p>Each attribute in the model represents a column in the table.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.db import modelsclass Product(models.Model):<br>    name = models.CharField(max_length=100)<br>    price = models.DecimalField(max_digits=10, decimal_places=2)<br>    stock = models.IntegerField()<br>    created_at = models.DateTimeField(auto_now_add=True)    def __str__(self):<br>        return self.name<\/pre>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Product is a table<\/li>\n\n\n\n<li>name, price, stock, created_at are columns<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Common Model Fields<\/h2>\n\n\n\n<p>CharField \u2192 Text with limited length<br>TextField \u2192 Large text<br>IntegerField \u2192 Whole numbers<br>DecimalField \u2192 Decimal numbers<br>FloatField \u2192 Floating point numbers<br>BooleanField \u2192 True or False<br>DateField \u2192 Date<br>DateTimeField \u2192 Date and time<br>EmailField \u2192 Email validation<\/p>\n\n\n\n<p>Each field has options like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>max_length<\/li>\n\n\n\n<li>default<\/li>\n\n\n\n<li>null<\/li>\n\n\n\n<li>blank<\/li>\n\n\n\n<li>unique<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How to Apply Models to Database<\/h2>\n\n\n\n<p>After creating or modifying models, run migrations.<\/p>\n\n\n\n<p>Step 1: Create migration file<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python manage.py makemigrations<\/pre>\n\n\n\n<p>Step 2: Apply migration<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python manage.py migrate<\/pre>\n\n\n\n<p>This creates the database table automatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Database Configuration<\/h2>\n\n\n\n<p>Django uses SQLite by default.<\/p>\n\n\n\n<p>In settings.py:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">DATABASES = {<br>    'default': {<br>        'ENGINE': 'django.db.backends.sqlite3',<br>        'NAME': BASE_DIR \/ 'db.sqlite3',<br>    }<br>}<\/pre>\n\n\n\n<p>You can also use:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PostgreSQL<\/li>\n\n\n\n<li>MySQL<\/li>\n\n\n\n<li>SQL Server<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">CRUD Operations Using Django ORM<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Create Data<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">Product.objects.create(name=\"Laptop\", price=80000, stock=10)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Read Data<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">Product.objects.all()<br>Product.objects.get(id=1)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Update Data<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">product = Product.objects.get(id=1)<br>product.price = 75000<br>product.save()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Delete Data<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">product = Product.objects.get(id=1)<br>product.delete()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Relationships Between Models<\/h2>\n\n\n\n<p>Django supports relationships:<\/p>\n\n\n\n<p>ForeignKey \u2192 One-to-Many<br>ManyToManyField \u2192 Many-to-Many<br>OneToOneField \u2192 One-to-One<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Category(models.Model):<br>    name = models.CharField(max_length=100)class Product(models.Model):<br>    name = models.CharField(max_length=100)<br>    category = models.ForeignKey(Category, on_delete=models.CASCADE)<\/pre>\n\n\n\n<p>This connects Product to Category.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Django Admin Integration<\/h2>\n\n\n\n<p>To manage models in Admin:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.contrib import admin<br>from .models import Productadmin.site.register(Product)<\/pre>\n\n\n\n<p>Now you can manage data from the admin panel.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Models Are Important<\/h2>\n\n\n\n<p>Models help you:<\/p>\n\n\n\n<p>Define data structure<br>Work with databases easily<br>Avoid writing complex SQL<br>Maintain clean code<br>Build scalable applications<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Takeaway<\/h2>\n\n\n\n<p>Models are the foundation of database management in Django.<br>They define your data structure and allow you to interact with the database using Python instead of raw SQL, making development faster and more efficient.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FOR WEB DEVELOPMENT (PYWEB) > Django Framework > Models and Database<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773719253760\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":86,"template":"","class_list":["post-158","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Models and Database - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Django models and ORM: define tables, perform CRUD operations, create relationships, and manage data via the admin panel.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/models-and-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Models and Database - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Django models and ORM: define tables, perform CRUD operations, create relationships, and manage data via the admin panel.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/models-and-database\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-17T03:45:17+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/models-and-database\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/models-and-database\\\/\",\"name\":\"Models and Database - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-03T03:49:09+00:00\",\"dateModified\":\"2026-03-17T03:45:17+00:00\",\"description\":\"Learn Django models and ORM: define tables, perform CRUD operations, create relationships, and manage data via the admin panel.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/models-and-database\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/models-and-database\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/models-and-database\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FOR WEB DEVELOPMENT (PYWEB) > Django Framework > Models and Database\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Models and Database - One Language. Endless Possibilities","description":"Learn Django models and ORM: define tables, perform CRUD operations, create relationships, and manage data via the admin panel.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/models-and-database\/","og_locale":"en_US","og_type":"article","og_title":"Models and Database - One Language. Endless Possibilities","og_description":"Learn Django models and ORM: define tables, perform CRUD operations, create relationships, and manage data via the admin panel.","og_url":"https:\/\/gigz.pk\/python\/lesson\/models-and-database\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-17T03:45:17+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/models-and-database\/","url":"https:\/\/gigz.pk\/python\/lesson\/models-and-database\/","name":"Models and Database - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-03T03:49:09+00:00","dateModified":"2026-03-17T03:45:17+00:00","description":"Learn Django models and ORM: define tables, perform CRUD operations, create relationships, and manage data via the admin panel.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/models-and-database\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/models-and-database\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/models-and-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FOR WEB DEVELOPMENT (PYWEB) > Django Framework > Models and Database"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}